【问题标题】:How do I access the views inside the layout when I reuse it multiple times?多次重用布局时如何访问布局内的视图?
【发布时间】:2010-08-06 07:41:07
【问题描述】:

我已经阅读了 Android 开发者的 Android UI 技巧 2,它告诉人们如何在另一个布局文件中多次包含一个布局,并为这些包含的布局赋予不同的 id。但是,这里的示例覆盖了布局 id,而不是此布局中视图的 id。例如,如果 workspace_screen.xml 如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:id="@+id/firstText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="first"/>
<TextView android:id="@+id/secondText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="second"/>

我在另一个布局文件中包含了 3 次。我最终会得到三个 id 为 firstText 的 TextView,以及另外三个 id 为 secondText 的 TextView 吗?不是id冲突吗?以及如何使用 findViewById 在第三个包含的布局中找到 secondText TextView?我应该在 findViewById 方法中输入什么?

【问题讨论】:

    标签: android layout include


    【解决方案1】:

    假设你想包含这个:

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="horizontal"
    >
        <ImageView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:src="@drawable/some_image"
        />
        <TextView
            android:id="@+id/included_text_view"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
        />
    </LinearLayout>
    

    所以在你的代码中你可以这样插入:

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="vertical"
    >
        <include android:id="@+id/header_1" layout="@layout/name_of_layout_xml" />
        <include android:id="@+id/header_2" layout="@layout/name_of_layout_xml" />
    </LinearLayout>
    

    现在您想要访问包含的布局中的文本视图以动态设置文本。在您的代码中,您只需键入:

    LinearLayout ll = (LinearLayout)findViewById(R.id.header_1);
    TextView tv = (TextView)ll.findViewById(R.id.included_text_view);
    tv.setText("Header Text 1");
    
    ll = (LinearLayout)findViewById(R.id.header_2);
    tv = (TextView)ll.findViewById(R.id.included_text_view);
    tv.setText("Header Text 2");
    

    请注意,您使用各个 LinearLayouts 的 findViewById 方法将搜索范围缩小到只有它们的子级。

    【讨论】:

      【解决方案2】:

      我想创建 TableRow 布局并想在 for 循环中将其重用到我的代码中。我一直在寻找问题,终于找到了解决方案。

              TableLayout table = (TableLayout) findViewById(R.id.listTable);
              LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              ImageButton[] button = new ImageButton[4];
              TableRow[] row = new TableRow[4];
      
              for(int i =0 ; i<4;i++){
      
                      row[i] = (TableRow) inflater.inflate(R.layout.list_row, null);
                      table.addView(row[i]);
                      button[i] = (ImageButton)row[i].findViewById(R.id.editBtn);
                      button[i].setOnClickListener(new OnClickListener() {
      
                          @Override
                          public void onClick(View v) {
                              // TODO Auto-generated method stub
                              Log.i(TAG,"Button Clicked");
                          }
                      });
      

      这样既可以实现布局复用的功能,也可以访问内部元素

      【讨论】:

        猜你喜欢
        • 2016-10-27
        • 2013-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多