自定义标题android onClick,调用了错误的id
我已经为我的活动实现了自定义标题:自定义标题android onClick,调用了错误的id,android,onclick,title,Android,Onclick,Title,我已经为我的活动实现了自定义标题: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#3a589
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#3a5894" >
<LinearLayout
android:layout_width="50dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:id="@+id/left_button_title_bar_main_content_fragment"
android:background="#90C3D4"
android:onClick="onClickTitleBar">
<ImageView android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#90C3D4"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:id="@+id/center_textview_title_bar"
android:onClick="onClickTitleBar">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="15dp"
android:text="Quick Notes"
android:textColor="#ffffff"/>
</LinearLayout>
</RelativeLayout>
以及我想处理单击的方法
public void onClickTitleBar(View v) {
int id = v.getId();
Log.e("","id : "+id);
Log.e("","button id: " + R.id.left_button_title_bar_main_content_fragment);
}
我已经注销了v.getId和linearlayout的实际id。
我意识到ID是不一样的
以下是日志:
E/﹕ id 2131493001
E/﹕ button id: 2131493000
正如你所看到的,数字是不一样的
知道为什么会这样吗
非常感谢您的帮助。在您的
xml
文件中,两个线性布局的onClick
属性的值相同。
可能是因为这个原因,你得到了不同的ID
尝试为两个线性布局指定不同的
onClick
。您已经使用IDR.ID.center\u textview\u title\u条设置了线性布局上的onClick。因为它位于相对位置,并且没有设置任何约束,并且它按照子对象的顺序“向上”,所以它将显示在线性布局的顶部,IDR.ID。左按钮\u标题栏\u主内容\u片段
,它将拾取代替标题栏的所有单击
若要解决此问题,请删除该行
android:onClick="onClickTitleBar"
从带有IDR.ID.center\u textview\u title\u条的线性布局中
android:onClick="onClickTitleBar"