如何以编程方式更改LinearLayout的重力?

时间:2012-02-29 18:55:33

标签: java android android-layout

我遇到水平线性布局的问题,我有一系列像瓷砖一样的图像。我有时会显示3个瓷砖,有时甚至是5个。当我只需要显示3时,我以编程方式将2个瓷砖设置为“已消失”。我需要将它们显示为居中,但仅当我显示3个瓷砖时。我的布局是什么样的:

<HorizontalScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="none">
    <!-- container -->
    <LinearLayout
        android:id="@+id/tile_container"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center">

        <!-- tile1 -->
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">           
            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"
                android:src="@drawable/img_panel"/>                 
        </FrameLayout>

        <!-- tile2 -->
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"
                android:src="@drawable/img_panel"/>
        </FrameLayout>  

                    <!-- tile3 -->
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">           
            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"
                android:src="@drawable/img_panel"/>                 
        </FrameLayout>

        <!-- tile4 -->
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"
                android:src="@drawable/img_panel"/>
        </FrameLayout>      

                    <!-- tile5 -->
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">           
            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"
                android:src="@drawable/img_panel"/>                 
        </FrameLayout>
                      </LinearLayout>
                         </HorizontalScrollView>

我尝试通过在代码中执行此操作来修复它,但我得到一个ClassCastException,因为它被Horizo​​ntalScrollView包装。有人有主意吗?

 ViewGroup tileContainer = (ViewGroup)this.findViewById(R.id.tile_container);
 ViewGroup.LayoutParams lp = new     LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,Gravity.CENTER);
 tileContainer.setLayoutParams(lp);

1 个答案:

答案 0 :(得分:0)

替换:

ViewGroup tileContainer = (ViewGroup)this.findViewById(R.id.tile_container);

使用:

LinearLayout tileContainer = (LinearLayout) this.findViewById(R.id.tile_container);
相关问题