【问题标题】:Issue Filling Parent View When Inflating Linear Layout膨胀线性布局时出现填充父视图的问题
【发布时间】:2023-03-06 22:06:01
【问题描述】:

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

这是我当前活动的样子。中心有一个水平滚动视图,我想通过将它们放在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 中的方式:

这是它的模型

<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 布局时;我得到不同的结果。这就是我试图扩大布局的方式:

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


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

【问题讨论】:

  • LinearLayoutOutlined 后面应该跟包名,比如 yourpackagensme。线性布局大纲。
  • @SanjeetAjnabee 我只是将它放在一个名为 android.widget 的包中,所以我在打字时可能会很懒惰。它肯定会被进口。
  • 您的 layout_holder LinearLayout 设置为水平方向。
  • @Devsil 有错吗?我正在尝试使 block 项目填充屏幕宽度,然后在它们开始扩展到屏幕外时滚动。因此,Horizo​​ntalScrollView 中的水平 LinearLayout
  • @flkes 我要给你写一个答案。希望我能帮到你。

标签: java android android-layout layout-inflater layoutparams


【解决方案1】:

在测量到LinearLayout 的高度后尝试充气并添加视图。下面我报告了您的 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);
        }
    });
}

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

希望这会有所帮助。

【讨论】:

  • 非常感谢!仍然有点混乱,但这解决了我的主要问题。一般来说,如果您在代码中实例化视图,您是否不使用 xml 高度/宽度?块中的所有内部项目也必须重新调整大小才能工作。
  • @flkes 好吧,问题是直接在onCreate 方法中添加视图意味着尝试将其包含到尚未呈现的布局中。在布局上使用post 方法可以让您等到它被渲染后再获取它的大小。不幸的是,match_parent 属性仍然存在一些问题。顺便说一句,在你的情况下,你可以使用layout_height="0dp"layout_weight="1"in inner LinearLayoutOutlined of block.xml
  • @flkes 很高兴我能帮上忙! ...好吧,所以我认为您可以接受答案:)
【解决方案2】:

您正在将布局添加到设置为方向=水平的 layout_holder。所以当然每个块都以水平方式与前一个块相邻。将该 layout_holder 设置为“vertical”会将每个块置于前一个块之下。

布局的 layout_width 决定了视图(组)的宽度,因此将其设置为“wrap_content”将允许它尽可能宽。只要确保您的块设置为与您需要的一样宽的 minimumWidth(或设置该内容,以便它包装尽可能宽的内容)。

【讨论】:

  • 我需要为我认为的最后一张照片拍一张新照片。我只有一张试图添加 5 个块的图片,这一定是误导。问题不在于它们如何排列,而在于每个街区的高度。我将尽快更新图像,只膨胀一个块。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多