【发布时间】:2012-05-26 19:51:35
【问题描述】:
我需要在左侧有一些带有可绘制对象的文本,并且我想在用户单击/触摸图像时执行一些代码(只有图像,而不是文本),所以我使用了 LinearLayout em> 带有一个 TextView 和一个 ImageView ,可点击并启动 onClick 事件。 XML 解析器建议我用带有 compound drawable 的 TextView 替换它,这样可以用更少的 XML 行来绘制相同的东西。 . 我的问题是“我可以指定我只想在 TextView 的可绘制对象上而不是在 TextView 本身上处理 onClick 事件吗?我已经看到了一些解决方案涉及编写自己的 TextView 扩展,但我只对能够在布局资源中执行此操作感兴趣,如果可能的话,否则我将保留以下 XML代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/home_feedback_title"
android:textColor="@android:color/primary_text_dark"
android:textStyle="bold"
android:paddingBottom="4dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/action_feedback"
android:clickable="true"
android:onClick="onClickFeedback"
android:contentDescription="@string/action_feedback_description"/>
</LinearLayout>
【问题讨论】:
标签: android android-layout onclick textview