【问题标题】:Alignment not right when adding view dynamically to GridLayout将视图动态添加到 GridLayout 时对齐不正确
【发布时间】:2015-11-20 07:41:47
【问题描述】:

当通过 XML 添加按钮时 - 很好

  <GridLayout
    android:id="@+id/social_gl_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:alignmentMode="alignBounds"
    android:columnCount="2"
    android:padding="8dp">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="45dp"
        android:layout_columnWeight="1">
        <Button
            android:layout_width="fill_parent"
            android:layout_height="45dp"
            android:background="@android:color/holo_blue_light"
            android:text="Hi"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="45dp"
        android:layout_columnWeight="1">
        <Button
            android:layout_width="fill_parent"
            android:layout_height="45dp"
            android:background="@android:color/holo_green_light"
            android:text="Whatsapp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="45dp"
        android:layout_columnWeight="1">
        <Button
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:background="@android:color/holo_green_light"
            android:text="This is facebook"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="45dp"
        android:layout_columnWeight="1">
        <Button
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:background="@android:color/holo_blue_light"
            android:text="On"
            />
    </LinearLayout>

</GridLayout>

当动态添加按钮时(通过代码) - 缺少对齐,按钮不占据一列的完整宽度

GridLayout gl = (GridLayout) findViewById(R.id.social_gl_content);
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    for (int i = 0 ; i < strs.length ; i++) {
        View v = inflater.inflate(R.layout.grid_item, null);
        Button b = (Button) v.findViewById(R.id.button);
        b.setText(strs[i]);
        if ( i % 2 ==0) {
            b.setBackgroundColor(Color.BLACK);
        }else{
            b.setBackgroundColor(Color.BLUE);
        }
        gl.addView(v);
    }

【问题讨论】:

  • 试试这个 LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); b.setLayoutParams(parms);

标签: android android-layout grid-layout android-gridlayout


【解决方案1】:

而不是传递null:

View v = inflater.inflate(R.layout.grid_item, null);

传递 parentView 以便 v 具有正确的布局参数。

View v = inflater.inflate(R.layout.grid_item, gl, false);

【讨论】:

  • 我复制了原来的问题,然后我尝试了这个解决方案,结果是一个空白屏幕。
【解决方案2】:

试试这个:

LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
b.setLayoutParams(parms);

【讨论】:

    猜你喜欢
    • 2012-02-06
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2023-03-22
    • 2018-10-21
    • 2017-11-28
    • 1970-01-01
    相关资源
    最近更新 更多