你好,我有很多这样的类别(超过8 xml布局),他们有相同的结构,像这样
这是我的布局文件夹中category_architecture.xml的一个示例:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout_architecture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:background="@drawable/category_architecture_background">
<ImageView
android:id="@+id/imageView_architecture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:src="@mipmap/ic_architecture" />
<TextView
android:id="@+id/textView_architecture"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_alignBottom="@+id/imageView_architecture"
android:layout_alignTop="@+id/imageView_architecture"
android:layout_toRightOf="@+id/imageView_architecture"
android:gravity="center"
android:text="@string/architecture"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColor="@android:color/white" />
<ImageView
android:id="@+id/imageView_building"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView_architecture"
android:layout_toRightOf="@+id/switch_building"
android:src="@mipmap/ic_building" />
<TextView
android:id="@+id/textView_building"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView_building"
android:layout_alignParentRight="true"
android:layout_below="@+id/imageView_architecture"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/imageView_building"
android:gravity="center_vertical|left"
android:text="@string/building"
android:textAlignment="viewStart"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@android:color/white" />
<android.support.v7.widget.SwitchCompat
android:id="@+id/switch_building"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_alignBottom="@+id/imageView_building"
android:layout_alignTop="@+id/imageView_building"
android:layout_toRightOf="@+id/imageView_architecture" />
<ImageView
android:id="@+id/imageView_bridge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView_building"
android:layout_toRightOf="@+id/switch_building"
android:src="@mipmap/ic_bridge" />
<TextView
android:id="@+id/textView_bridge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView_bridge"
android:layout_alignParentRight="true"
android:layout_alignLeft="@+id/textView_building"
android:layout_below="@+id/textView_building"
android:gravity="center_vertical|left"
android:text="@string/bridge"
android:textAlignment="viewStart"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@android:color/white" />
<android.support.v7.widget.SwitchCompat
android:id="@+id/switch_bridge"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_above="@+id/imageView_monument"
android:layout_below="@+id/textView_building"
android:layout_toRightOf="@+id/imageView_architecture" />
<ImageView
android:id="@+id/imageView_monument"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView_bridge"
android:layout_toRightOf="@+id/switch_building"
android:src="@mipmap/ic_monument" />
<TextView
android:id="@+id/textView_monument"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imageView_airport"
android:layout_alignParentRight="true"
android:layout_alignLeft="@+id/textView_building"
android:layout_below="@+id/textView_bridge"
android:gravity="center_vertical|left"
android:text="@string/Monument"
android:textAlignment="viewStart"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@android:color/white" />
<android.support.v7.widget.SwitchCompat
android:id="@+id/switch_monument"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_alignBottom="@+id/imageView_monument"
android:layout_below="@+id/imageView_bridge"
android:layout_toRightOf="@+id/imageView_architecture" />
<android.support.v7.widget.SwitchCompat
android:id="@+id/switch_airport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView_airport"
android:layout_below="@+id/switch_monument"
android:layout_toRightOf="@+id/imageView_architecture" />
<ImageView
android:id="@+id/imageView_airport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView_monument"
android:layout_toRightOf="@+id/switch_building"
android:src="@mipmap/ic_airport" />
<TextView
android:id="@+id/textView_airport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView_airport"
android:layout_alignParentRight="true"
android:layout_alignLeft="@+id/textView_building"
android:layout_below="@+id/textView_monument"
android:gravity="center_vertical|left"
android:text="@string/airport"
android:textAlignment="viewStart"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@android:color/white" />
<android.support.v7.widget.SwitchCompat
android:id="@+id/switch_park_attraction"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_alignBottom="@+id/imageView_park_attraction"
android:layout_below="@+id/switch_airport"
android:layout_toRightOf="@+id/imageView_architecture" />
<ImageView
android:id="@+id/imageView_park_attraction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView_airport"
android:layout_toRightOf="@+id/switch_building"
android:src="@mipmap/ic_amusement" />
<TextView
android:id="@+id/textView_park_attraction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView_park_attraction"
android:layout_alignParentRight="true"
android:layout_alignLeft="@+id/textView_building"
android:layout_alignTop="@+id/imageView_park_attraction"
android:gravity="center_vertical|left"
android:text="@string/amusment"
android:textAlignment="viewStart"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@android:color/white" />
<android.support.v7.widget.SwitchCompat
android:id="@+id/switch_infrastructure"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_alignBottom="@+id/imageView_infrastructure"
android:layout_below="@+id/switch_park_attraction"
android:layout_toRightOf="@+id/imageView_architecture" />
<ImageView
android:id="@+id/imageView_infrastructure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView_park_attraction"
android:layout_toRightOf="@+id/switch_building"
android:src="@mipmap/ic_infrastructure" />
<TextView
android:id="@+id/textView_infrastructure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView_infrastructure"
android:layout_alignParentRight="true"
android:layout_alignLeft="@+id/textView_building"
android:layout_below="@+id/textView_park_attraction"
android:gravity="center_vertical|left"
android:text="@string/infrastructure"
android:textAlignment="viewStart"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@android:color/white" />
</RelativeLayout>
所以我对这个布局实现有一些问题:
布局实现没问题,一切正常(切换监听器,在触发一条线时,在searchview上更改picutre,根据searchview和appbar的类别和颜色切换appbar图标...
但是我认为有很多提示可以避免重复代码重复......所有类别布局都需要做很多工作,而且我想确定在改变之前我做了什么主要方法......(我不想花几个小时在灌木丛中殴打而没有任何正确的结果......)
P:我很抱歉在这里乞讨,但我想收集一些外部建议,以便改进我的代码并为将来的工作学习这些,谢谢;)答案 0 :(得分:1)
你绝对应该使用RecyclerView(或ListView)
尝试将您的布局视为具有不同单元格布局的列表:&#34;自然标题&#34;,&#34;自然单元&#34;,&#34;地理标题&#34; ...
然后,使用正常的RecyclerView机制,它应该是直接的。
这个解释的例子将对您有所帮助: