无法在android studio中将权重参数添加到片段?

无法在android studio中将权重参数添加到片段?,android,layout,Android,Layout,我试图在AndroidStudio中将权重参数添加到片段xml中,这样我的两个片段可以各占半个屏幕,但添加权重对布局没有影响。有趣的是,我仍然可以为正常的活动视图添加权重 这是我的布局文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.andr

我试图在AndroidStudio中将权重参数添加到片段xml中,这样我的两个片段可以各占半个屏幕,但添加权重对布局没有影响。有趣的是,我仍然可以为正常的活动视图添加权重

这是我的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="io.designcoder.vivz.lifecycle.xml.ActivityFragmentXml">

<fragment
    android:id="@+id/fragment_xml_a"
    class="io.designcoder.vivz.lifecycle.xml.FragmentXmlA"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    tools:layout="@layout/fragment_xml_a"
    />
<fragment
    android:id="@+id/fragment_xml_b"
    class="io.designcoder.vivz.lifecycle.xml.FragmentXmlB"
    android:layout_width="match_parent"
    android:layout_below="@id/fragment_xml_a"
    android:layout_height="wrap_content"
    tools:layout="@layout/fragment_xml_b"
    />

</RelativeLayout>

您尝试使用以下代码:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="cungxunu.cunghoangdao.cheng.myapplication.MainActivity">

    <fragment
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>
    <fragment
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>
</LinearLayout>

您不应该使用相对布局,因为权重在那里不起作用。 因此,用户线性布局


请记住:权重参数只能在线性布局中工作。如果要让两个片段水平共享屏幕,请将linearlayout的方向设置为水平,并将每个片段的宽度设置为“0dp”或“wrap_content”,以便对其进行称重。垂直方向也是这样。例如,您可以在下面查看我的代码。


//背景仅用于区分片段

将片段放在线性布局中。并将碎片宽度设为0dp。你的碎片是在LinearLayout内还是在RelativeLayout内?请共享布局文件。权重仅在LinearLayout中有效。您使用的是RelativeLayout!请在答案中明确地提供代码片段。答案必须更多地集中在这个话题上,而不是提供一些一般性的东西。谢谢,@dysaniazz,这更好,让我投赞成票。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="horizontal"
    tools:context="com.example.dysaniazzz.testdemo.MainActivity">

   //the background only to distinguish the fragment
   <fragment
       android:layout_width="wrap_content"
       android:layout_height="match_parent"
       android:layout_weight="1"
       android:background="@android:color/holo_red_light" />
  <fragment
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@android:color/holo_green_light" />
</LinearLayout>