2017-04-15 35 views
2

我已經爲我的應用程序創建了一個顏色目錄,並決定使用一個Array適配器來充氣此目錄。我的顏色佈局如下:ArrayAdapter的ListView的Android佈局優化:是FrameLayout真的無用嗎?

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="80dp" 
     android:layout_marginLeft="20dp" 
     android:layout_marginStart="20dp" 
     android:layout_marginTop="20dp" 
     android:layout_marginBottom="20dp" 
     android:layout_marginRight="40dp" 
     android:layout_marginEnd="40dp"> 

     <ImageView 
      android:id="@+id/fav_swatch" 
      android:layout_width="80dp" 
      android:layout_height="match_parent"/> 

     <TextView 
      android:id="@+id/fav_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@+id/fav_swatch" 
      android:layout_toEndOf="@+id/fav_swatch" 
      android:paddingLeft="30dp" 
      android:paddingStart="30dp" 
      android:paddingTop="10dp" 
      android:textSize="20sp" 
      /> 

     <TextView 
      android:id="@+id/fav_coord" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@+id/fav_swatch" 
      android:layout_toEndOf="@+id/fav_swatch" 
      android:layout_below="@+id/fav_name" 
      android:paddingLeft="30dp" 
      android:paddingStart="30dp" 
      android:paddingTop="10dp" 
      android:textSize="20sp" 
      /> 

     <ImageButton 
      android:id="@+id/btn_delete" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentEnd="true" 
      android:layout_marginLeft="10dp" 
      android:background="@drawable/ic_clear"/> 

     <ImageButton 
      android:id="@+id/btn_modify" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toLeftOf="@+id/btn_delete" 
      android:layout_toStartOf="@+id/btn_delete" 
      android:background="@drawable/ic_edit"/> 

    </RelativeLayout> 

</FrameLayout> 

我的活動佈局:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fav_fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" /> 

    <ListView 
     android:id="@+id/fav_lv" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

    </ListView> 

</FrameLayout> 

所以這個代碼將導致所需的佈局,因爲這:

Correct layout

的問題是,Android的警告我

這個RelativeLayout或其父FrameLayout是沒用的

這是不是真的沒用,因爲如果我只保留RelativeLayout父:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="80dp" 
    android:layout_marginLeft="20dp" 
    android:layout_marginStart="20dp" 
    android:layout_marginTop="20dp" 
    android:layout_marginBottom="20dp" 
    android:layout_marginRight="40dp" 
    android:layout_marginEnd="40dp" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

     <ImageView 
      android:id="@+id/fav_swatch" 
      android:layout_width="80dp" 
      android:layout_height="match_parent"/> 

     <TextView 
      android:id="@+id/fav_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@+id/fav_swatch" 
      android:layout_toEndOf="@+id/fav_swatch" 
      android:paddingLeft="30dp" 
      android:paddingStart="30dp" 
      android:paddingTop="10dp" 
      android:textSize="20sp" 
      /> 

     <TextView 
      android:id="@+id/fav_coord" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@+id/fav_swatch" 
      android:layout_toEndOf="@+id/fav_swatch" 
      android:layout_below="@+id/fav_name" 
      android:paddingLeft="30dp" 
      android:paddingStart="30dp" 
      android:paddingTop="10dp" 
      android:textSize="20sp" 
      /> 

     <ImageButton 
      android:id="@+id/btn_delete" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentEnd="true" 
      android:layout_marginLeft="10dp" 
      android:background="@drawable/ic_clear"/> 

     <ImageButton 
      android:id="@+id/btn_modify" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toLeftOf="@+id/btn_delete" 
      android:layout_toStartOf="@+id/btn_delete" 
      android:background="@drawable/ic_edit"/> 

</RelativeLayout> 

邊緣會被忽略,我得到這種醜陋的佈局:

Undesired layout

我的問題是:有沒有什麼辦法可以得到正確的佈局,而不必PU t'沒用'FrameLayout得到一個更清潔和更優化的代碼?

編輯:有趣的Java代碼:

// Construct the data source 
     ArrayList<Colour> arrayOfColours = new ArrayList<>(); 

     final ColourAdapter adapter = new ColourAdapter(this, arrayOfColours); 

     ListView lv = (ListView) findViewById(R.id.fav_lv); 
     lv.setAdapter(adapter); 

     final Colour[] colour = new Colour[ca.getMaxNumberOfFavColours()]; 
     for (int i = 0; i < numberOfColours; i++) { 
      colour[i] = new Colour(colourPixel[i], colourName[i]); 
      adapter.add(colour[i]); 
     } 

回答

1

在您的方案FrameLayout是包含一個子元素爲RelativeLayout。您的所有其他元素都包含在RelativeLayout之內。可能這是Android Studio給你的警告。

此RelativeLayout或其父FrameLayout是無用的。

我不明白你的觀點FrameLayout裏面有RelativeLayout。相反,我也建議使用其中之一。

而且,你可以通過在你的第二個場景中這樣得到同樣的結果...

  1. RelativeLayout去除餘量。
  2. 代替保證金,在RelativLayout中加android:padding="20dp"

讓我知道你是否有任何疑問與此相關。

我會建議你see the diff. in margin and padding,並避免給你的佈局的最外層容器保證金。

PS:開發人員在出現錯誤之前不會太在意警告。 :p

+0

填充是關鍵。我想我仍然需要更好地瞭解佈局屬性。謝謝! –

+0

這些佈局的東西,有時候會造成很大的混亂。 –