繁体   English   中英

当在片段旁边添加另一个片段时,缩放片段(FrameLayout)

[英]Scale a fragment (FrameLayout) when another one is added next to it

我有一个包含ExpandableListView的片段。 现在,当单击一个孩子时,我想在ListView旁边添加另一个片段。 ListView应该缩放,以便新的片段正确适合。

这是我的布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.test"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="-10dp"
        android:layout_marginLeft="-10dp"
        android:layout_marginRight="-10dp"
        android:background="?attr/spinner_background"
        android:gravity="center">
        <FrameLayout
            android:id="@+id/fragment_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:id="@+id/fragment_menu"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"/>

        <FrameLayout
            android:id="@+id/fragment_content"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"/>
    </TableRow>
</LinearLayout>

我添加列表片段是这样的:

fragmentTransaction.add(R.id.fragment_menu, fragment, "fragMenu").commit();

然后在子项上单击我添加另一个片段:

fragmentTransaction.add(R.id.fragment_content, fragment, "fragContent").commit();

添加另一个片段时,有什么方法可以缩放第一个片段? 我是否需要通过编程设置宽度?

请注意,如果我将特定宽度设置为2个FrameLayouts,则可以正常工作。

您想让两个FrameLayout共享TableRow的总空间吗?

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TableRow
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="-10dp"
    android:layout_marginLeft="-10dp"
    android:layout_marginRight="-10dp"
    android:background="?attr/spinner_background"
    android:gravity="center">
    <FrameLayout
        android:id="@+id/fragment_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</TableRow>

<TableRow
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:id="@+id/fragment_menu"
            android:layout_width="0dp"
            android:layout_weight=1
            android:layout_height="match_parent"/>

        <FrameLayout
            android:id="@+id/fragment_content"
            android:layout_width="0dp"
            android:layout_weight=1
            android:layout_height="match_parent"
            android:visibility="gone"/>
    </LinearLayout>
</TableRow>

然后,添加此行

findViewById(R.id.fragment_content).setVisibility(View.VISIBLE);
fragmentTransaction.add(R.id.fragment_content, fragment, "fragContent").commit();

这将使第二个容器可见, LinearLayout将在布局过程中自动为每个容器分配等效的空间(由于layout_weight属性)。

PS:停止使用fill_parent ,自API 8起已弃用...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM