简体   繁体   中英

How can I programatically change the gravity for a LinearLayout?

I'm having issues with a horizontal linear layout where I have a series of images that are like tiles. I sometimes display 3 of the tiles and sometimes 5. I programatically set 2 of the tiles as "gone" when I only need to show 3. I need to display them centered, but only when I am displaying 3 tiles. what my layout looks like:

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

I tried to fix it by doing this in the code but I get a ClassCastException seemingly because it's wrapped by a HorizontalScrollView. Anyone have any ideas?

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

Replace:

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

with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM