Issue Filling Parent View When Inflating Linear Layout

自作多情 提交于 2019-12-13 05:31:33

问题


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

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>

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

And this is model for it

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

This is what it looks like when it gets inflated with a block:

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.


回答1:


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:

Hope this could help.




回答2:


You are adding layouts to your layout_holder which is set to orientation=horizontal. So of course every block goes next to the previous one in a horizontal fashion. Setting that layout_holder to "vertical" will put each block under the previous one.

The layout_width of a layout determines how wide the view(group) is, so setting that to "wrap_content" will allow it to be as wide as it needs to be. Just be sure your blocks are set to a minimumWidth which is as wide as you need it to be (or have that content setup so that it wraps content which is as wide as it needs to be).



来源:https://stackoverflow.com/questions/35120890/issue-filling-parent-view-when-inflating-linear-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!