2016-07-26 64 views
-2

我想添加重量參數到片段xml在android工作室,這樣我的兩個fragements可以每個半屏幕,但增加重量沒有影響佈局。什麼有趣的是,我仍然可以增加重量正常活動的意見在這裏是screen shot無法在android studio中添加重量參數到片段?

這是我的佈局文件

<?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> 
+0

把你的片段放在LinearLayout中。並讓你的片段寬度0dp –

+0

你的片段是在LinearLayout還是RelativeLayout中? – Natalia

+0

請分享您的佈局文件。 – Joshua

回答

2

記住:重量PARAM只能在LinearLayout中工作。如果要讓這兩個片段在水平方向共享屏幕,請將linearlayout的方向設置爲水平,並將每個片段的寬度設置爲「0dp」或「wrap_content」,以便可以對它們進行加權。垂直方向也是一樣。例如,您可以查看我的代碼。 the weight example

<?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> 
+0

請在回答中明確提供代碼片段。答案必須更專注於該主題,而不是提供一些通用的。 – Farside

+1

@Farside好的,我已經粘貼了代碼。 – DysaniazzZ

+0

謝謝@DysaniazzZ,這樣好多了,得到我的最高票數。 – Farside

0

您嘗試使用下面的代碼:

<?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> 
0

你不應該使用相對佈局作爲重量不會在那裏工作。 因此,用戶線性佈局。

<?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:weightSum="2" 
 
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="0dp" 
 
    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="0dp" 
 
    tools:layout="@layout/fragment_xml_b" 
 
    /> 
 

 
</LinearLayout>