Как я могу программно изменить гравитацию для LinearLayout? - PullRequest
0 голосов
/ 29 февраля 2012

У меня проблемы с горизонтальной линейной разметкой, когда у меня есть серия изображений, которые похожи на плитки. Иногда я отображаю 3 плитки, а иногда 5. Я программно устанавливаю 2 плитки как «пропавшие», когда мне нужно только показать 3. Мне нужно отображать их по центру, но только когда я показываю 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, по-видимому, потому что он обернут в HorizontalScrollView. У кого-нибудь есть идеи?

 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 голосов
/ 29 февраля 2012

Заменить:

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

с:

LinearLayout tileContainer = (LinearLayout) this.findViewById(R.id.tile_container);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...