发行灌装父视图当充气线性布局 [英] Issue Filling Parent View When Inflating Linear Layout

查看:307
本文介绍了发行灌装父视图当充气线性布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的 LinearLayoutOutlined 类是这个答案只有一些小的修改颜色。

The LinearLayoutOutlined class I'm using is from this answer with only a few small edits for color.

这是我目前的活动看起来像。有一个在中心的水平滚动视图,我想通过把他们在 layout_holder 来填充项目。这是页面的XML布局:

This is what my current activity looks like. There is a horizontal scroll view in the center that I want to fill with items by putting them in the layout_holder. This is the xml layout for the page:

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


该娄形象是我多么希望布局将膨胀成layout_holder:


The bellow image is how I want block layouts to be inflated into layout_holder:

在这里输入的形象描述

这是因为它的模型

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

但是试图动态添加块时布局;我得到不同的结果。这是我正在试图夸大的布局:

However when trying to dynamically add the block layout; I get a different result. This is how I'm trying to inflate the 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);

这是个什么样子,当它被用块充气,如:

在这里输入的形象描述

最嵌套的 LinearLayoutOutlined 布局不与我在模型中定义的布局匹配起来。它的高度似乎是零时,它应该是从屏幕的顶部的距离,直到连接按钮的顶部。任何想法,我做错了吗?

The most nested LinearLayoutOutlined in the block layout is not matching up with the layouts I defined in the model. It's height appears to be zero when it should be the distance from the top of the screen until the top of the "connect" button. Any idea what I'm doing wrong here?

修改拍了更好的屏幕截图,以更好地解释这个问题。

EDIT Took a better screenshot to explain the issue better.

推荐答案

尝试膨胀和的LinearLayout 的高度之后添加的观点得到了测量。下面我报告的执行你的活动的onCreate 方法。

Try to inflate and add View after height of LinearLayout has been measured. Below I reported implementation for onCreate method of your Activity.

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

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

This is how it looks on my device:

在这里输入的形象描述

希望这可以帮助。

这篇关于发行灌装父视图当充气线性布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆