Фрагменты из двух половинок в одном макете одна под другой

#android #native

#Android #родной

Вопрос:

Это мой XML-код. 1-й фрагмент — scanner_fragment для сканирования штрих-кода. 2-й фрагмент — content_fragment для отображения результатов сканирования штрих-кода и кнопок отправки внутри него.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="wrap_content" >



    <fragment android:name="me.dm7.barcodescanner.zxing.sample.SimpleScannerFragment"
        android:id="@ id/scanner_fragment"
        android:layout_width="match_parent"
        android:layout_weight="0.5"
        android:layout_height="300dp" />



    <fragment
        android:id="@ id/content_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"




        />


</RelativeLayout>
  

Я хочу поместить фрагмент содержимого чуть ниже фрагмента сканера

Я хочу поместить больше представлений, таких как listview, внутри content_fragment. как это преодолеть?

Получилось! Вот изменения ниже.

activity_scan.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_weight="1"
    android:orientation="vertical"
    android:layout_height="wrap_content">


    <fragment android:name="com.example."
        android:id="@ id/scanner_fragment"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="300dp" />


    <fragment
        android:id="@ id/content_fragment"
        android:layout_width="match_parent"
        android:layout_height="356dp"
        android:layout_weight="1"
        tools:layout="@layout/scan_result"/>


</LinearLayout>
  

И для инструментов:layout=»@layout/scan_result»
scan_result.xml

 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">




    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:layout_marginBottom="600dp">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="fill_horizontal">

    <Button
        android:id="@ id/reset"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="20dp"
        android:layout_weight="1"
        android:text="Reset" />


    <Button
        android:id="@ id/confirm"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Confirm" />


    </LinearLayout>

    </RelativeLayout>

</LinearLayout>
  

Ответ №1:

-Используйте линейную компоновку вместо относительной компоновки

-Для линейной компоновки поместите android:weightSum="2"

-Каждый макет фрейма должен иметь android:layout_weight="1"

Ответ №2:

Используйте линейную компоновку и присвойте это свойство всем framLayout

 android:layout_height="0dp"
android:layout_weight="1"
  

Я надеюсь, что это поможет вам!

Спасибо.

Комментарии:

1. Я хочу фрагмент содержимого ниже фрагмента сканера

Ответ №3:

Используйте атрибут layout below.

    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <fragment android:name="me.dm7.barcodescanner.zxing.sample.SimpleScannerFragment"
        android:id="@ id/scanner_fragment"
        android:layout_width="match_parent"
        android:layout_height="300dp" />


    <fragment
        android:id="@ id/content_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/scanner_fragment"
        />
</RelativeLayout>
  

Комментарии:

1. Я хочу фрагменты один под другим, в то время как этот код выдает фрагменты, смежные с другим, которые мне не нужны.

2. Я решил это сам, вам тоже спасибо. Смотрите изменения выше.