动态将相对布局添加到线性布局中

6
我在 DrawerLayout 内部有一个名为 "container" 的 LinearLayout。运行时,我试图在 "container" 中添加一个RelativeLayout 。这会导致 RelativeLayout 的对齐出现问题,即进度条覆盖了标志图片。 相对布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    android:keepScreenOn="true">
    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/logo" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/logo"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingStart="8dp"
        android:paddingEnd="5dp">


        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="1dp"
            android:visibility="gone" />

        <TextView
            android:id="@+id/tv_network_error"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:gravity="center"
            android:text="@string/no_network"
            android:textColor="#E10000"
            android:textSize="30sp"
            android:visibility="visible" />

    </LinearLayout>

    <TextView
        android:id="@+id/tv_software_version"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:paddingRight="20dp"
        android:paddingBottom="20dp"
        android:text="Version"
        android:textColor="@android:color/darker_gray" />
</RelativeLayout>

抽屉布局(DrawerLayout)含有容器
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="false">
    <LinearLayout
        android:id="@+id/contentPanel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:background="@color/white"
        android:fitsSystemWindows="false"
        app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>

在运行时注入布局。
protected View getRootView(View view) {
        View sliderLayout = LayoutInflater.from(this).inflate(R.layout.slider_layout, null);
        LinearLayout layout = (LinearLayout) sliderLayout.findViewById(R.id.contentPanel);
        layout.addView(view);
        return sliderLayout;
    }

你能使用布局检查器在应用程序运行时查看实际布局吗?如果这没有帮助:检查器应该生成一个检索到的布局文件,你可以向我们展示以获得更进一步的帮助。 - JensV
1
由于您在RelativeLayout中将Logo ImageView放置在ProgressBar之前,因此希望看到ProgressBar覆盖在Logo上方。 - aminography
2个回答

3

我不确定在你的代码中 getRootView() 位于何处。如果你澄清一下,我可以提供更好的解决方案。

然而,我创建了一个带有导航抽屉活动的项目,在 layout_to_be_added.xml 中定义了要动态添加的 RelativeLayout,并在 MainActivity 的 onCreate() 中进行了以下实验:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ...

    final LinearLayout contentPanel = findViewById(R.id.contentPanel);
    contentPanel.post(new Runnable() {
        @Override
        public void run() {
            View layoutToBeAdded = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_to_be_added, null);
            contentPanel.addView(layoutToBeAdded);
            // contentPanel.invalidate();
        }
    });
}

这导致了您提到的问题,进度条和文本不在居中的标志下方,如下图所示:

Probably the layout params are not correct....

看起来空根会导致错误的计算或评估,因此我将inflate()行更新如下:

View layoutToBeAdded = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_to_be_added, contentPanel, false);

在这里,我们将容器作为根容器,但我们不将滑块布局附加到它上面。这样,根容器仅用于计算正确的LayoutParams,我们可以得到如下所示的预期结果:

Correct result


你在方法的第一行从哪里得到了对象“sliderLayout”?视图“contentPanel”位于sliderLayout的内部视图中。 - Sneha Bansal
我已经删除了它,因为这只是一个瞎猜,我不知道 getRootView 在哪里以及你的整个实现方式是怎样的。不过这个解决方案对于我的例子来说是有效的。 - Mehmed
在调用getRootView()之前,必须先调用setContentView()方法。请确保已经完成了这一步骤。 - Sneha Bansal
你不能在Activity中重写getRootView,所以我想知道你在哪里重写它了。 - Mehmed
这不是被覆盖的方法,而是我创建的一个方法,将在父类的setContentView()中被调用。 - Sneha Bansal
无论如何,感谢您的努力,并从您的角度得到了一些提示。因此,接受答案。 - Sneha Bansal

0

我已经修改了下面的这行代码

contentPanel.addView(view);

contentPanel.addView(view,LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);

这对我有用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接