2013-10-22 37 views
0

我有一個RelativeLayout父母,它的內部有ImageButton s,排列爲layout_below,layout_alignRight等。這給了我預期的佈局。android相對佈局中的可摺疊元素

this

我希望能夠摺疊一些這些按鈕,以便後面的按鈕移動一個空間「向上」。如果我用gone隱藏了一個或多個按鈕的可見性,整個事情就會變得不合時宜,因爲它們彼此相對佈局。我不能求助於GridView,因爲我必須能夠在API 7+上運行。

所以基本上,我正在尋找像CSS浮動物的東西。這是否可以通過RelativeLayout實現或者其他佈局更好?

這裏的佈局(簡體):

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="?theme_background" > 

    <RelativeLayout 
     android:id="@+id/headerLayout" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="5dip" 
     android:background="?theme_header_background"> 

    <ImageButton 
     android:id="@+id/imgBtnAbout" 
     android:layout_width="30dip" 
     android:layout_height="30dip" 
     android:background="@drawable/ic_action_about" 
     android:layout_alignParentLeft="true" 
     android:scaleType="fitXY" 
     /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/header" 
     android:text="App Name" 
     android:textSize="24sp" 
     android:textStyle="bold" 
     android:textColor="?theme_header_text" 
     android:layout_centerInParent="true" /> 

    <ImageButton 
     android:id="@+id/imgBtnSetting" 
     android:layout_width="30dip" 
     android:layout_height="30dip" 
     android:background="@drawable/ic_action_settings" 
     android:layout_alignParentRight="true"/> 

    </RelativeLayout> 

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:fillViewport="true" 
     android:orientation="vertical" 
     android:background="?theme_background" 
     android:layout_below="@+id/headerLayout"> 

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:weightSum="1.0" 
      tools:context=".MainActivity"> 

      <ImageButton 
       android:layout_width="fill_parent" 
       android:layout_height="100dp" 
       android:id="@+id/imBtn1" 
       android:scaleType="centerCrop" 
       android:background="#222" 
       android:layout_margin="5dp" /> 

      <ImageButton 
       android:layout_width="fill_parent" 
       android:layout_height="100dp" 
       android:id="@+id/imBtn2" 
       android:layout_below="@+id/imBtn1" 
       android:scaleType="centerCrop" 
       android:background="#222" 
       android:layout_margin="5dp" /> 

      <ImageButton 
       android:layout_width="fill_parent" 
       android:layout_height="50dp" 
       android:id="@+id/imBtn3" 
       android:layout_below="@+id/imBtn2" 
       android:scaleType="centerCrop" 
       android:background="@null" 
       android:layout_margin="5dp" /> 

      <View android:id="@+id/fakeView1" 
       android:layout_width="0dp" 
       android:layout_height="0dp" 
       android:layout_centerInParent="true" 
       android:layout_below="@+id/imBtnAd" /> 

      <ImageButton 
       android:layout_width="fill_parent" 
       android:layout_height="100dp" 
       android:id="@+id/imBtn4" 
       android:layout_alignRight="@+id/fakeView1" 
       android:layout_alignTop="@+id/fakeView1" 
       android:scaleType="centerCrop" 
       android:adjustViewBounds="true" 
       android:background="#222" 
       android:layout_margin="5dp"/> 

      <ImageButton 
       android:layout_width="fill_parent" 
       android:layout_height="100dp" 
       android:id="@+id/imBtn5" 
       android:scaleType="centerCrop" 
       android:adjustViewBounds="true" 
       android:background="#222" 
       android:layout_alignLeft="@+id/fakeView1" 
       android:layout_alignTop="@+id/fakeView1" 
       android:layout_margin="5dp"/> 

      <View android:id="@+id/fakeView2" 
       android:layout_width="0dp" 
       android:layout_height="0dp" 
       android:layout_centerInParent="true" 
       android:layout_below="@+id/imBtnBiz" /> 

      <ImageButton 
       android:layout_width="fill_parent" 
       android:layout_height="100dp" 
       android:id="@+id/imBtn6" 
       android:scaleType="centerCrop" 
       android:adjustViewBounds="true" 
       android:background="#222" 
       android:layout_alignRight="@+id/fakeView2" 
       android:layout_alignTop="@+id/fakeView2" 
       android:layout_alignParentLeft="true" 
       android:layout_margin="5dp"/> 

      <ImageButton 
       android:layout_width="fill_parent" 
       android:layout_height="100dp" 
       android:id="@+id/imBtn7" 
       android:scaleType="centerCrop" 
       android:adjustViewBounds="true" 
       android:background="#222" 
       android:layout_alignLeft="@+id/fakeView2" 
       android:layout_alignTop="@+id/fakeView2" 
       android:layout_margin="5dp"/> 

      <View android:id="@+id/fakeView3" 
       android:layout_width="0dp" 
       android:layout_height="0dp" 
       android:layout_centerInParent="true" 
       android:layout_below="@+id/imBtnAuto" /> 

      <ImageButton 
       android:layout_width="fill_parent" 
       android:layout_height="100dp" 
       android:id="@+id/imBtn8" 
       android:scaleType="centerCrop" 
       android:adjustViewBounds="true" 
       android:background="#222" 
       android:layout_alignRight="@+id/fakeView3" 
       android:layout_alignTop="@+id/fakeView3" 
       android:layout_margin="5dp"/> 

      <ImageButton 
       android:layout_width="fill_parent" 
       android:layout_height="100dp" 
       android:id="@+id/imBtn9" 
       android:scaleType="centerCrop" 
       android:adjustViewBounds="true" 
       android:background="#222" 
       android:layout_alignLeft="@+id/fakeView3" 
       android:layout_alignTop="@+id/fakeView3" 
       android:layout_margin="5dp"/> 

      <View android:id="@+id/fakeView4" 
       android:layout_width="0dp" 
       android:layout_height="0dp" 
       android:layout_centerInParent="true" 
       android:layout_below="@+id/imBtnHumor" /> 

      <ImageButton 
       android:layout_width="fill_parent" 
       android:layout_height="100dp" 
       android:id="@+id/imBtn10" 
       android:scaleType="centerCrop" 
       android:adjustViewBounds="true" 
       android:background="#222" 
       android:layout_alignRight="@+id/fakeView4" 
       android:layout_alignTop="@+id/fakeView4" 
       android:layout_margin="5dp"/> 

     </RelativeLayout> 

    </ScrollView> 
</RelativeLayout> 
+1

發佈你的XML可能會有所幫助。但有可能你必須在這個裏面使用nestead'LinearLayout'。 – Androiderson

+0

對問題 – sostacked

回答

1

也許你可以選擇另一種策略:不要試圖隱藏按鈕,但只顯示所需的按鈕來代替。你可以這樣編程:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if(showButtonB1) { 
    Button b1 = new Button(this); 
    //then set button image here 
    layout.addView(b1); 
    } 

    if(showOtherButton){ 
    //...the same 
    } 

    //when all buttons needed were created: 
    setContentView(layout); 
} 
+0

添加了佈局很可能會這樣,只是尋找一個「更清潔」的解決方案。感謝您的意見。 – sostacked