是否可以在一个布局中制作两个具有相同ID的按钮?

时间:2015-02-20 03:06:07

标签: android android-layout android-button

是否可以在一个布局中制作两个ID相同的按钮?

我尝试通过标记<include>,但不适用于侦听器其中一个按钮

3 个答案:

答案 0 :(得分:2)

编辑 2016:

这不再可能,android lint检查阻止你在同一个xml文件中添加相同的id。 解决方法是使用include标记来包含具有重复ID的其他xmls。 然而,这是一种应该避免的方法。

OLD ANSWER

TL:DR;您只能在View容器中使用distince ID,如果再创建一个容器,则可以使用parent.findViewById(R.id.child_id)引用它;

虽然您的用例在设计级别上似乎存在缺陷,但这是可能的

 <LinearLayout
    android:id="@+id/parent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:padding="30dp">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
<RelativeLayout
android:id="@+id/button_2"
android: layout_width="match_parent"
android:layout_height="match_parent">
<include
    android:id="@+id/second_buttonLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    layout="@layout/title_bar" />
</RelativeLayout>
</LinearLayout>

为两个按钮设置监听器,

  findViewById(R.id.button1); //referring to first button
((RelativeLayout)findViewById(R.id.button_2)).findViewById(R.id.button_of_include) //referring to second button

编辑基于OP's answer关键点正在为包含include的父布局提供ID。这样可以引用布局因此它的子布局。即:

<RelativeLayout
        android:id="@+id/button_2"
        android: layout_width="80dp"
        android:layout_height="80dp">

            <include
                 android:layout_width="80dp"
                 android:layout_height="80dp"
                 layout="@layout/include_layout" />
    </RelativeLayout>

答案 1 :(得分:0)

这是不可能的。但是,如果我想将相同的侦听器绑定到某些等于视图,则可以使用标记。例如:

<ImageView
                android:id="@+id/map_button_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:tag="button"
                android:src="@drawable/button" />

添加方法以按标记获取所有视图:

private static ArrayList<View> getViewsByTag(ViewGroup root, String tag){
ArrayList<View> views = new ArrayList<View>();
final int childCount = root.getChildCount();
for (int i = 0; i < childCount; i++) {
    final View child = root.getChildAt(i);
    if (child instanceof ViewGroup) {
        views.addAll(getViewsByTag((ViewGroup) child, tag));
    }

    final Object tagObj = child.getTag();
    if (tagObj != null && tagObj.equals(tag)) {
        views.add(child);
    }

    }
    return views;
}

现在就像使用它一样

List<View> myViews = getViewsByTag(rootView, "button");

答案 2 :(得分:0)

我这样做了

<LinearLayout
    android:id="@+id/parent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="30dp">

    <Button
        android:id="@+id/button_1"
        android:layout_width="80dp"
        android:layout_height="80dp"
         />

    <RelativeLayout
        android:id="@+id/button_2"
        android: layout_width="80dp"
        android:layout_height="80dp">

            <include
                 android:layout_width="80dp"
                 android:layout_height="80dp"
                 layout="@layout/include_layout" />
    </RelativeLayout>
</LinearLayout>

<强> inlcude_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"        
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <Button
        android:id="@+id/button_1" // the same id
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
         />
</RelativeLayout>

在我班上

Btn[0] = (Button).findViewById(R.id.button_1);      
Btn[1] = (Button)((RelativeLayout).findViewById(R.id.button_2)).findViewById(R.id.button_1);

结果是在一个布局中放置两个带有一个ID的按钮,只有一个听众!

项目https://github.com/kgsnick/TwoButtonWithOneId