Android 3 - 将片段添加到 LinearLayout:fill_parent 不起作用

发布于 2024-12-28 21:07:55 字数 2589 浏览 0 评论 0原文

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

另一方面, fill_parent 作用于片段的宽度。 我怎样才能改变这种行为?

DashboardActivity:

<?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;
        }

I am trying to add a fragment programmatically to a LinearLayout, but the fragment does not stretch its content across the whole layouts height. It acts like wrap_content instead of fill_parent.

On the other side the fill_parent works on the fragment's width.
How can I change this behaviour?

DashboardActivity:

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

Its onCreate method:

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();
    } 

The 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>

And its createView:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙女山的月亮 2025-01-04 21:07:55

好吧——这个非常愚蠢。我将容器从 LinearLayout 更改为 FrameLayout ,瞧:它正在工作。

Ok - this one was pretty dumb. I changed my container from a LinearLayout to a FrameLayout and voilà: it's working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文