2017-04-15 34 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

这些布局的东西,有时候会造成很大的混乱。 –