我想实现一个应用程序来存储有关冰柜内容的信息。 我开发的第一个布局是5" floor" ,在任何楼层都有12个Button和12个TextView,但结果是12x5 = 60个按钮和60个textview。
我认为这种布局有很多资源......或者不是吗? 代码实现起来更简单,但性能不好!(我认为)。
这是第一个实现的代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbarStyle="insideOverlay"
android:scrollbars="vertical" >
<LinearLayout
android:id="@+id/LinearLayout0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="@drawable/a9p_09_11_00943" >
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:columnCount="8"
android:orientation="horizontal"
android:rowCount="4" >
<!-- prima riga P1 R1 -->
<ImageButton
android:id="@+id/btnP1C1R1"
android:layout_column="1"
android:layout_gravity="left|top"
android:layout_row="0"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<ImageButton
android:id="@+id/btnP1C2R1"
android:layout_column="3"
android:layout_gravity="left|top"
android:layout_row="0"
android:background="@drawable/add_64" android:contentDescription="@string/store_add"/>
<ImageButton
android:id="@+id/btnP1C3R1"
android:layout_column="5"
android:layout_gravity="left|top"
android:layout_row="0"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<TextView
android:id="@+id/txtP1C1R1"
android:layout_width="@dimen/size_txt_view"
android:layout_column="2"
android:layout_gravity="left|center_vertical"
android:layout_row="0"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C2R1"
android:layout_width="@dimen/size_txt_view"
android:layout_column="4"
android:layout_gravity="left|center_vertical"
android:layout_row="0"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C3R1"
android:layout_width="@dimen/size_txt_view"
android:layout_column="6"
android:layout_gravity="left|center_vertical"
android:layout_row="0"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<!-- seconda riga P1 R2 -->
<ImageButton
android:id="@+id/btnP1C1R2"
android:layout_column="1"
android:layout_gravity="left|top"
android:layout_row="1"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<ImageButton
android:id="@+id/btnP1C2R2"
android:layout_column="3"
android:layout_gravity="left|top"
android:layout_row="1"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<ImageButton
android:id="@+id/btnP1C3R2"
android:layout_column="5"
android:layout_gravity="left|top"
android:layout_row="1"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<TextView
android:id="@+id/txtP1C1R2"
android:layout_column="2"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="1"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C2R2"
android:layout_column="4"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="1"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C3R2"
android:layout_column="6"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="1"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<!-- seconda riga P1 R3 -->
<ImageButton
android:id="@+id/btnP1C1R3"
android:layout_column="1"
android:layout_gravity="left|top"
android:layout_row="2"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<ImageButton
android:id="@+id/btnP1C2R3"
android:layout_column="3"
android:layout_gravity="left|top"
android:layout_row="2"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<ImageButton
android:id="@+id/btnP1C3R3"
android:layout_column="5"
android:layout_gravity="left|top"
android:layout_row="2"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<TextView
android:id="@+id/txtP1C1R3"
android:layout_column="2"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="2"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C2R3"
android:layout_column="4"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="2"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C3R3"
android:layout_column="6"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="2"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<!-- seconda riga P1 R4 -->
<ImageButton
android:id="@+id/btnP1C1R4"
android:layout_column="1"
android:layout_gravity="left|top"
android:layout_row="3"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<ImageButton
android:id="@+id/btnP1C2R4"
android:layout_column="3"
android:layout_gravity="left|top"
android:layout_row="3"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<ImageButton
android:id="@+id/btnP1C3R4"
android:layout_column="5"
android:layout_gravity="left|top"
android:layout_row="3"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<TextView
android:id="@+id/txtP1C1R4"
android:layout_column="2"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="3"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C2R4"
android:layout_column="4"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="3"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C3R4"
android:layout_column="6"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="3"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_column="0"
android:layout_row="0"
android:layout_gravity="left|center_vertical"
android:text="@string/store_4" />
<TextView
android:id="@+id/textView2"
android:layout_column="0"
android:layout_row="1"
android:layout_gravity="left|center_vertical"
android:text="@string/store_3" />
<TextView
android:id="@+id/textView3"
android:layout_column="0"
android:layout_row="2"
android:layout_gravity="left|center_vertical"
android:text="@string/store_2" />
<TextView
android:id="@+id/textView4"
android:layout_column="0"
android:layout_row="3"
android:layout_gravity="left|center_vertical"
android:text="@string/store_1" />
</GridLayout>
</LinearLayout>
第二个想法是自定义ListView。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/a9p_09_11_00943">
<RelativeLayout
android:id="@+id/RelativeLayout0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true">
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true" >
</ListView>
</RelativeLayout>
</RelativeLayout>
自定义项目包含12个buttom和12个textview,如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbarStyle="insideOverlay"
android:scrollbars="vertical" >
<LinearLayout
android:id="@+id/LinearLayout0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="@drawable/a9p_09_11_00943" >
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:columnCount="8"
android:orientation="horizontal"
android:rowCount="4" >
<!-- prima riga P1 R1 -->
<ImageButton
android:id="@+id/btnP1C1R1"
android:layout_column="1"
android:layout_gravity="left|top"
android:layout_row="0"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<ImageButton
android:id="@+id/btnP1C2R1"
android:layout_column="3"
android:layout_gravity="left|top"
android:layout_row="0"
android:background="@drawable/add_64" android:contentDescription="@string/store_add"/>
<ImageButton
android:id="@+id/btnP1C3R1"
android:layout_column="5"
android:layout_gravity="left|top"
android:layout_row="0"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<TextView
android:id="@+id/txtP1C1R1"
android:layout_width="@dimen/size_txt_view"
android:layout_column="2"
android:layout_gravity="left|center_vertical"
android:layout_row="0"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C2R1"
android:layout_width="@dimen/size_txt_view"
android:layout_column="4"
android:layout_gravity="left|center_vertical"
android:layout_row="0"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C3R1"
android:layout_width="@dimen/size_txt_view"
android:layout_column="6"
android:layout_gravity="left|center_vertical"
android:layout_row="0"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<!-- seconda riga P1 R2 -->
<ImageButton
android:id="@+id/btnP1C1R2"
android:layout_column="1"
android:layout_gravity="left|top"
android:layout_row="1"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<ImageButton
android:id="@+id/btnP1C2R2"
android:layout_column="3"
android:layout_gravity="left|top"
android:layout_row="1"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<ImageButton
android:id="@+id/btnP1C3R2"
android:layout_column="5"
android:layout_gravity="left|top"
android:layout_row="1"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<TextView
android:id="@+id/txtP1C1R2"
android:layout_column="2"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="1"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C2R2"
android:layout_column="4"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="1"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C3R2"
android:layout_column="6"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="1"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<!-- seconda riga P1 R3 -->
<ImageButton
android:id="@+id/btnP1C1R3"
android:layout_column="1"
android:layout_gravity="left|top"
android:layout_row="2"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<ImageButton
android:id="@+id/btnP1C2R3"
android:layout_column="3"
android:layout_gravity="left|top"
android:layout_row="2"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<ImageButton
android:id="@+id/btnP1C3R3"
android:layout_column="5"
android:layout_gravity="left|top"
android:layout_row="2"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<TextView
android:id="@+id/txtP1C1R3"
android:layout_column="2"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="2"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C2R3"
android:layout_column="4"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="2"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C3R3"
android:layout_column="6"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="2"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<!-- seconda riga P1 R4 -->
<ImageButton
android:id="@+id/btnP1C1R4"
android:layout_column="1"
android:layout_gravity="left|top"
android:layout_row="3"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<ImageButton
android:id="@+id/btnP1C2R4"
android:layout_column="3"
android:layout_gravity="left|top"
android:layout_row="3"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<ImageButton
android:id="@+id/btnP1C3R4"
android:layout_column="5"
android:layout_gravity="left|top"
android:layout_row="3"
android:background="@drawable/add_64" android:contentDescription="@string/store_add" />
<TextView
android:id="@+id/txtP1C1R4"
android:layout_column="2"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="3"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C2R4"
android:layout_column="4"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="3"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtP1C3R4"
android:layout_column="6"
android:layout_width="@dimen/size_txt_view"
android:layout_gravity="left|center_vertical"
android:layout_row="3"
android:text="@string/store_void"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_column="0"
android:layout_row="0"
android:layout_gravity="left|center_vertical"
android:text="@string/store_4" />
<TextView
android:id="@+id/textView2"
android:layout_column="0"
android:layout_row="1"
android:layout_gravity="left|center_vertical"
android:text="@string/store_3" />
<TextView
android:id="@+id/textView3"
android:layout_column="0"
android:layout_row="2"
android:layout_gravity="left|center_vertical"
android:text="@string/store_2" />
<TextView
android:id="@+id/textView4"
android:layout_column="0"
android:layout_row="3"
android:layout_gravity="left|center_vertical"
android:text="@string/store_1" />
</GridLayout>
</LinearLayout>
</RelativeLayout>
我有可能增加很多楼层,更多的是5! 但是实现代码有很多困难: 1)当用户点击项目(冷冻地板)时,如何为所有12个按钮实现 button.setOnClickListener ?
2)适配器内 getView 的性能是快还是慢?
另一个想法是创建5片段,并在每个片段内部构建一个xml whith 12按钮和12个textview。 可能这个想法还是更复杂? 还有其他想法吗? 感谢。
修改
我尝试了另一种方法,通过对xml充气:
容器:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbars="vertical" >
</LinearLayout>
代码:
private static final int[] BUTTON_IDS_P1 = {
R.id.btnP1C1R1,
R.id.btnP1C1R2,
R.id.btnP1C1R3,
R.id.btnP1C1R4,
R.id.btnP1C2R1,
R.id.btnP1C2R2,
R.id.btnP1C2R3,
R.id.btnP1C2R4,
R.id.btnP1C3R1,
R.id.btnP1C3R2,
R.id.btnP1C3R3,
R.id.btnP1C3R4
};
for(int i = 0; i < 5; i++){
final LayoutInflater inflater = (LayoutInflater) appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View child = inflater.inflate(R.layout.ripiano_frigo, null);
child.setId(i);
lLayout = (LinearLayout) rootView.findViewById(R.id.LinearLayout0);
lLayout.addView(child);
buttons = new ArrayList<ImageButton>(BUTTON_IDS_P1.length);
int idt=0;
for(int id : BUTTON_IDS_P1) {
ImageButton button = (ImageButton)rootView.findViewById(id);
//button.setOnClickListener(this); // maybe
MyTag myTag = new MyTag(idt, id,TEXTVIEW_IDS_P1[idt++], 0, 0);
button.setTag(myTag);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
对于第一组按钮(12),一切正常,但如何配置另一组?哪个是第二组按钮的ID?
答案 0 :(得分:1)
使用第一层创建布局并动态扩展布局并创建楼层。