[英]Android layout not clickable when include ImageView and TextView
我有一個如下的布局。 共有五個類似的標簽。 當我在代碼中將onClickListener設置為LinearLayout tab_dashboard時,什么也沒有觸發。 我還必須將點擊偵聽器設置為ImageButton和TextView,以使整個區域都可單擊。 我試圖將所有子項設置為clickable =“ false”和diplicateParentState =“ true”,但不走運。 我也嘗試將ImageButton更改為ImageView,同樣的問題。 有人對此有想法嗎?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/tab_dashboard"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/white"
android:gravity="center"
android:orientation="vertical">
<ImageButton
android:id="@+id/dashboard_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@android:color/white"
android:src="@drawable/ic_dashboard" />
<TextView
android:id="@+id/dashboard_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="@string/dashboard"
android:textSize="10sp" />
</LinearLayout>
<!-- Four more similar tabs -->
</LinearLayout
向TextView和ImageView添加android:clickable =“ false”和android:focusable =“ false”
在您的活動中添加以下代碼。
((ImageView) findViewById(R.id.dashboard_button)).setOnClickListener(null);
((ImageView) findViewById(R.id.dashboard_button)).setFocusable(false);
((TextView) findViewById(R.id.dashboard_text)).setOnClickListener(null);
((TextView) findViewById(R.id.dashboard_text)).setFocusable(false);
我通過用實現相同效果的其他方式替換了android:layout_width =“ 0dp”和android:layout_weight =“ 1”解決了該問題。 我猜android:layout_width =“ 0dp”可能是造成此問題的原因。 稍后將進行更多調查。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.