[英]Android converting from LinearLayout to CheckedTextView and TextView
[英]CheckedTextView in LinearLayout
我有下面的XML。 我想使imageview可点击。 但是,看来我只能单击checkedTextView。 有没有一种方法可以让我独立检查clickedtextview和imageview? 似乎checkedTextView占用了整个空间,而imageview则位于checkedtextview后面(也许是?)。 有任何想法吗? 对于android开发而言,这是很新的东西,这已经很混乱了。 我尝试权重,但似乎根本没有帮助。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical">
<CheckedTextView
android:id="@+id/checked_text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center_vertical"
android:text="@string/hello_world"
android:textColor="@color/white"
android:padding="20dp"
android:textSize="20sp"
android:textStyle="bold"/>
<ImageView
android:id="@+id/my_image_pencil"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/image_pencil" />
</LinearLayout>
用这个...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="60dp" >
<CheckedTextView
android:id="@+id/checked_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center|right"
android:padding="20dp"
android:text="Hello"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/my_image_pencil"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
如果要在行项目中单击ImageView事件,请设置
android:focusable="false
为您的CheckedTextView 。
尝试这个,
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical">
<CheckedTextView
android:id="@+id/checked_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center_vertical"
android:text="@string/hello_world"
android:textColor="@color/white"
android:padding="20dp"
android:textSize="20sp"
android:textStyle="bold"/>
<ImageView
android:id="@+id/my_image_pencil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onclick="onImageClick"
android:src="@drawable/image_pencil" />
</LinearLayout>
活动中有这样的方法
public void onImageClick(View view)
{
// handler for imageclick
}
就像我们在列表视图中使用复选框,按钮,图像按钮等时一样。 它占据了listview的所有焦点,因此,当我们单击listview或listview的其他元素(例如图像等)时,就不会将焦点放在这些小部件上,因此不会对其执行可单击的操作。
因此,只需如下更改复选框的代码即可。
<CheckedTextView
android:id="@+id/checked_text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center_vertical"
android:text="@string/hello_world"
android:padding="20dp"
android:textSize="20sp"
android:focusable="false"
android:textStyle="bold"/>
我只是在您的代码中添加了这一行android:focusable="false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.