java - 膨胀线性布局时填充父 View 的问题

标签 java android android-layout layout-inflater layoutparams

我使用的 LinearLayoutOutlined 类来自 this answer只需对颜色进行一些小的编辑。

这就是我当前的 Activity 。中心有一个水平 ScrollView ,我想通过将项目放入 layout_holder 中来填充项目。这是页面的 xml 布局:

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ControlPanel"
        android:orientation="vertical"
        android:id="@+id/main">

    <HorizontalScrollView 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:fillViewport="true">

        <LinearLayout
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/layout_holder">

        </LinearLayout>
    </HorizontalScrollView>

    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom">

        <Button
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="@string/connect_button_title"
                android:id="@+id/button_button"
                android:layout_gravity="start|bottom"
                android:layout_width="0dp"
                android:onClick="connectServer"/>

        <EditText
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:id="@+id/editText"
                android:layout_gravity="end|bottom"
                android:text="@string/default_ip"
                android:layout_width="0dp"/>

    </LinearLayout>
</LinearLayout>
<小时/>

下面的图片是我希望将 block 布局膨胀到layout_holder中的方式:

enter image description here

这是它的模型

<LinearLayoutOutlined xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/block">

    <ImageView
            android:layout_marginTop="15dp"
            android:layout_height="50dp"
            android:layout_width="50dp"
            android:layout_gravity="top|center"/>

    <LinearLayoutOutlined
            android:orientation="vertical"
            android:layout_width="80dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal|fill_vertical"/>

</LinearLayoutOutlined>

但是,当尝试动态添加 block 布局时;我得到了不同的结果。这就是我尝试 inflated layout 的方式:

LayoutInflater layoutInfralte = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.layout_holder);
View block = layoutInfralte.inflate(R.layout.block, null);
linearLayout.addView(block);

这就是它用 block 充气时的样子:

enter image description here

block 布局中嵌套最多的 LinearLayoutOutlined 与我在模型中定义的布局不匹配。当它的高度应该是从屏幕顶部到“连接”按钮顶部的距离时,它的高度似乎为零。知道我在这里做错了什么吗?

<小时/>

编辑拍摄了更好的屏幕截图以更好地解释问题。

最佳答案

测量 LinearLayout 的高度后尝试膨胀并添加 View。下面我报告了 Activity 的 onCreate 方法的实现。

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

    final LayoutInflater li = LayoutInflater.from(this);
    final LinearLayout layout = (LinearLayout) findViewById(R.id.layout_holder);

    layout.post(new Runnable() {
        @Override
        public void run() {
            int height = layout.getMeasuredHeight();
            LinearLayoutOutlined view = (LinearLayoutOutlined) li
                .inflate(R.layout.block, null);
            view.setMinimumHeight(height);
            layout.addView(view);
        }
    });
}

这是它在我的设备上的外观:

enter image description here

希望这能有所帮助。

关于java - 膨胀线性布局时填充父 View 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35120890/

相关文章:

java - 如何在 tomcat 6 web.xml servlet-mapping servlet-pattern 中定义子路径

java - 如何在java中非繁琐地打印出10行10个字符(或字符串)

java.lang.IncompleteClassChangeError : the number of constructors

java - 如何使用 Java 以编程方式从 MS Exchange Server 获取电子邮件 header ?

java - Android webview 未加载本地主机服务器

android - 上方布局 - 居中其他布局

android - RelativeLayout如何给布局权重?

android - 如何在基于 Angular cordova 应用程序中正确处理 X-CSRF-TOKEN?

java - Android:创建一个处理所有蓝牙相关操作的类

android - 如何让两个ListView控件的高度适合UI?