如何在更改方向时使“线性布局”可滚动

时间:2016-03-12 19:53:20

标签: android android-layout android-scrollview android-orientation

我为App主菜单设计类似于Tile的结构。为此,我使用了一些重量的线性布局,以便Tiles(ImageButtons)正确排列。但当我将手机的方向更改为横向时,ImageButtons的宽度会发生变化(整个布局),以避免使用以下代码

   @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
            findViewById(R.id.activity_MainMenu).setPadding(400,0,400,-250);
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
            findViewById(R.id.activity_MainMenu).setPadding(16, 16, 16, 16);
        }
    }

现在,瓷砖排列正确,因为我从左右两侧填充400填充,从底部填充-250填充。但只显示了一半的按钮。为了避免这种情况,我添加了一个滚动视图但它似乎没有工作。

<ScrollView
        android:fadeScrollbars="true"
        android:fillViewport="true"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

     <LinearLayout
         android:id="@+id/activity_MainMenu"
         android:paddingBottom="@dimen/activity_vertical_margin"
         android:paddingTop="@dimen/activity_vertical_margin"
         android:paddingLeft="16dp"
         android:paddingRight="16dp"
         android:layout_width="match_parent"
         android:layout_height="match_parent">

         <LinearLayout
             android:id="@+id/LL1"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="horizontal"
             android:layout_weight="1">

             <LinearLayout
                 android:id="@+id/LL11"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:orientation="vertical">

                 <ImageButton
                     android:layout_width="match_parent"
                     android:layout_height="0dp"
                     android:layout_weight="1.5"
                     android:id="@+id/button_fitness"
                     android:scaleType="fitCenter"
                     />

                 <ImageButton
                     android:layout_width="match_parent"
                     android:layout_height="0dp"
                     android:layout_weight="1"
                      />

                 <ImageButton
                     android:layout_width="match_parent"
                     android:layout_height="0dp"
                     android:layout_weight="1"/>

             </LinearLayout>


         </LinearLayout>

         <LinearLayout
             android:id="@+id/LL2"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="vertical"
             android:layout_weight="1">


             <ImageButton
                 android:layout_width="match_parent"
                 android:layout_height="0dp"
                 android:layout_weight="1"/>

             <ImageButton
                 android:layout_width="match_parent"
                 android:layout_height="0dp"
                 android:layout_weight="1"
                 android:scaleType="fitCenter"/>


         </LinearLayout>


     </LinearLayout>

    </ScrollView> 

1 个答案:

答案 0 :(得分:1)

尝试从ScrollView中删除android:orientation =“horizo​​ntal”。

另外,为什么在1 LinearLayout中有2个LinearLayout,其中height = match_parent?

使用android:layout_weight =“1”,高度应为0,

<LinearLayout
         android:id="@+id/activity_MainMenu"
         android:orientation="vertical"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
      <LinearLayout
             android:id="@+id/LL1"
             android:layout_width="match_parent"
             android:layout_height="0"
             android:orientation="horizontal"
             android:layout_weight="1">
       </LinearLayout>
       <LinearLayout
             android:id="@+id/LL2"
             android:layout_width="match_parent"
             android:layout_height="0"
             android:orientation="horizontal"
             android:layout_weight="1">
       </LinearLayout>
</LinearLayout>