【问题标题】:Android 3 - Adding a Fragment to a LinearLayout: fill_parent does not workAndroid 3 - 向 LinearLayout 添加片段:fill_parent 不起作用
【发布时间】:2012-01-26 14:54:46
【问题描述】:

我正在尝试以编程方式将片段添加到 LinearLayout,但片段不会将其内容拉伸到整个布局高度。它的作用类似于 wrap_content 而不是 fill_parent。

另一方面,fill_parent 作用于片段的宽度。 我该如何改变这种行为?

仪表板活动:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/fragment_dashboard"
        android:layout_width="5dp"
        android:layout_height="fill_parent"
        android:layout_marginLeft="20dp"
        android:layout_weight="1"
        android:name="de.upb.cs.ginkgo.tablet.ui.fragments.DashboardFragment" >
        <!-- Preview: layout=@layout/fragment_dashboard -->
        </fragment>

    <LinearLayout
        android:id="@+id/rightfrag"
        android:layout_width="450dp" 
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="15dp"
        android:layout_marginRight="10dp" >
    </LinearLayout>

</LinearLayout>

它的onCreate方法:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);
    //-- Instantiate Fragmentstuff
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    ActivitystreamFragment asf = new ActivitystreamFragment();
    fragmentTransaction.add(R.id.rightfrag, asf);
    fragmentTransaction.commit();
    } 

ActivitystreamFragment XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">

    <ListView
        android:id="@+id/android:list"
        android:layout_width="0dp" 
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawSelectorOnTop="true"
        android:background="@drawable/back_rightfrag"
         >
</ListView>
</LinearLayout>

及其createView:

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_activitystream, null);
        return root;
        }

【问题讨论】:

    标签: android android-3.0-honeycomb android-fragments fill-parent


    【解决方案1】:

    好的 - 这个很愚蠢。我将容器从 LinearLayout 更改为 FrameLayout,然后瞧:它正在工作。

    【讨论】:

    • 谢谢。但我不知道为什么 FrameLayout 有效,但 LinearLayout 没有
    • 您是否尝试将 FrameLayout 放入 LinearLayout?
    • @NguyenMinhBinh:因为 FrameLayout 将其子级放在彼此之上,另一方面,LinearLayout 将其子级彼此相邻(水平或垂直)。转换确实有效,但新添加的 Fragment 不在显示范围内。
    • Grate catch :) 它可以工作,但我不知道在线性布局中获得此有线输出的原因是什么,再次感谢 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    • 2014-10-16
    • 1970-01-01
    • 2010-12-29
    相关资源
    最近更新 更多