【问题标题】:Issue with simple inflate to linear layout in android?android中简单的膨胀到线性布局的问题?
【发布时间】:2016-10-26 15:48:58
【问题描述】:

我有这个代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#ff00ff"
    android:id="@+id/candyBox">
</LinearLayout>

我想将以上内容填充到第 1、2、3、4 列布局中,如下所示:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="4"
    >
    <!-- 4 LAYERS of linear layout, ready to be inflated -->
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:weightSum="5"
        android:id="@+id/column1"
        android:gravity="bottom">

    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:weightSum="5"
        android:id="@+id/column2">

    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:weightSum="5"
        android:id="@+id/column3">

    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:weightSum="5"
        android:id="@+id/column4">

    </LinearLayout>
</LinearLayout>

我正在做这个简单的代码:

 LinearLayout column1 = (LinearLayout)findViewById(R.id.column1);
    View viewToLoad = getLayoutInflater().inflate(R.layout.candy_box, null);
    column1.addView(viewToLoad);

似乎什么也没发生,我的代码有问题吗?看起来很简单。如果我硬编码这件事,一切都很好,但我需要那些动态插入的,有一种“Java”方式,还有这个……我需要那样做,我不明白这是个问题和总和。

为了更清楚,我的最终结果应该是这样的:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:weightSum="5"
    android:id="@+id/column1"
    android:gravity="bottom">
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#ff00ff"
        android:id="@+id/candyBox">
    </LinearLayout>
    ...
</LinearLayout>

【问题讨论】:

  • 检查是否有异常。
  • 没有例外,我马上更新到我的完整代码

标签: android android-linearlayout layout-inflater


【解决方案1】:

当您的 LinearLayout 似乎是您的 rootView 元素时,您为什么要定义 android:layout_weight="1"

更新

看起来你倒了一些东西。您应该通过 id candy_box 找到并以编程方式设置 column1

试试这个。

LinearLayout candyBoxLayout = (LinearLayout)findViewById(R.id.candy_box);
View viewToLoad = getLayoutInflater().inflate(R.layout.column1, null);

candyBoxLayout.addView(viewToLoad);

【讨论】:

    【解决方案2】:

    我认为问题在于您的外部布局的权重 - weightSum 应该在外部布局中,而 layout_weight 应该在内部布局中。 为了确保您所做的事情是正确的,我还建议对内部的内部布局进行硬编码,并在 IDE 中查看图形设计页面中的布局。

    更新 2:我想要的应该是这样的:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="horizontal"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:weightSum="4"
                  android:background="#44000000"
        >
        <!-- 4 LAYERS of linear layout, ready to be inflated -->
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:weightSum="5"
            android:id="@+id/column1"
            android:gravity="bottom"
            android:background="#33ffffff">
    
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#ff00ff"
                android:id="@+id/candyBox1">
            </LinearLayout>
    
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#0000ff"
                android:id="@+id/candyBox2">
            </LinearLayout>
    
        </LinearLayout>
    
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:weightSum="5"
            android:id="@+id/column2"
            android:background="#22ffffff"
            android:gravity="bottom">
    
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="3"
                android:background="#ff0000"
                android:id="@+id/candyBox3">
            </LinearLayout>
        </LinearLayout>
    
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:weightSum="5"
            android:gravity="bottom"
            android:id="@+id/column3">
    
        </LinearLayout>
    
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:weightSum="5"
            android:gravity="bottom"
            android:id="@+id/column4">
    
        </LinearLayout>
    
    </LinearLayout>
    

    【讨论】:

    • weightSum 应该说明这一点
    • 没错,weightSum 是 5,我想把带 1 的那个加到父亲身上。如果我硬编码一切都很好,就看不到那里的问题
    • weightSum 应该放在外部视图中。
    • 相反! doron,我想让糖果在第 1 列的内部,请查看更新
    【解决方案3】:

    问题是xml的结构:

    我的布局不在根对象中,所以我必须执行以下操作:

         column1 = (LinearLayout)findViewById(R.id.column1);
         View v = getLayoutInflater().inflate(R.layout.candy_box,column1 ,false);
         column1.addView(v);
    

    false 是键,表示它不是根元素。

    解决了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      相关资源
      最近更新 更多