簡體   English   中英

Android:如何在內部使用LinearLayout創建自定義按鈕?

[英]Android: how to make a custom button with LinearLayout inside?

我有以下layout ,它已經用作ListView的一行:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/schedule_row_content"
    android:background="@drawable/item_selector" android:layout_width="fill_parent" android:layout_height="60dp">
    <TextView android:gravity="center_vertical|center_horizontal" android:layout_gravity="center_vertical" android:layout_height="wrap_content" android:textColor="#000000" android:id="@+id/labelLessonNumber" android:text="1" android:layout_width="27dp"></TextView>
    <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="10px" android:layout_weight="1" android:layout_gravity="center_vertical">
        <TextView android:layout_height="wrap_content" android:textColor="#000000" android:id="@+id/labelSubject" android:layout_width="fill_parent" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Text"></TextView>
        <TextView android:layout_height="wrap_content" android:textColor="#555555" android:layout_width="wrap_content" android:id="@+id/labelTime" android:text="00:00"></TextView>
    </LinearLayout>
    <LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent">
        <TextView android:gravity="center" android:width="80dp" android:layout_gravity="center_vertical" android:layout_height="wrap_content" android:textColor="#000000" android:layout_width="wrap_content" android:id="@+id/labelGroup" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Klasa Ia"></TextView>
    </LinearLayout>    
</LinearLayout>

我想使用此layout來創建自定義按鈕(與ListView內的工作方式相同)。 是否有任何容器可以從內部視圖捕獲onClick事件?

如果您只需要單擊按鈕,那么一個簡單而直接的解決方案就是采用線性布局,並將其背景設置為自定義選擇器drawble,然后在您的Java代碼中簡單設置此線性布局的onclick偵聽器。

自定義選擇器鏈接1

自定義選擇器鏈接2

在您的情況下,我們可以為所有列表項提供一個通用的單擊偵聽器,但可以通過為所有列表項按鈕設置不同的標記對象來進行區分。

希望對您有所幫助。

無需在按鈕中放置任何內容,您需要可點擊的Layout原封不動,因此為此使用此功能。

<LinearLayout
android:clickable="true"
android:background="@android:drawable/btn_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
    android:id="@+id/image"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:src="@android:drawable/ic_input_add"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</ImageView>
<TextView
    android:id="@+id/text"
    android:text="Sample text"
    android:textAppearance="?android:attr/textAppearanceButton"
    android:layout_toRightOf="@id/image"
    android:layout_centerVertical="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"></TextView>
</LinearLayout>

因此android:clickable =“ true”將使Layout可點擊,並且行為類似於按鈕:)希望對您有所幫助。

我不確定您是否可以將布局用作自定義按鈕,因為它的clickable標志應該設置為true。 如果這樣做,它將捕獲onClick事件,並且ListView會出現問題。 嘗試將android:clickable =“ true”設置為您的布局,並使用include標記:

<include android:id="@+id/your_layout"
     layout="@layout/your_layout"
 android:layout_height="wrap_content"
 android:layout_width="fill_parent"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM