2016-07-26 6 views
-2

アンドロイドスタジオのフラグメントxmlにウェイトパラメータを追加しようとしています.2つのフラグメントがそれぞれ半分のスクリーンを取ることができますが、ウェイトを追加するとレイアウトに影響はありません。どのような興味深い私はまだここでは、通常のアクティビティビューに重みを追加することができます。ここでscreen shotアンドロイドスタジオのフラグメントにウェイトパラメーターを追加できませんか?

は私のレイアウトファイルは

<?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で作業することができます。 2つのフラグメントが水平にスクリーンを共有するようにするには、直線レイアウトの向きを水平にし、各フラグメントの幅を "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>

関連する問題