有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在安卓中对相对布局产生连锁反应?

我已经使用相对布局为安卓中的卡视图创建了自定义按钮,并通过<;包括布局功能。一切都很好,我只是想知道如何在点击时产生连锁反应

这是我的密码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent">

    <FrameLayout
        安卓:layout_width="125dp"
        安卓:layout_height="30dp"
        安卓:background="@color/colorPrimary"
        安卓:layout_gravity="center_horizontal"
        安卓:focusableInTouchMode="true">


        <TextView
            安卓:layout_width="125dp"
            安卓:layout_height="30dp"
            安卓:fontFamily="@font/questa"
            安卓:text="Know More"
            安卓:gravity="center"
            安卓:textAllCaps="false"
            安卓:textColor="#ffffff">
        </TextView>

    </FrameLayout>

</RelativeLayout>

包括布局代码:

<include
                安卓:id="@+id/btnViewDetails"
                style="@style/CardView.Button"
                layout="@layout/button"
                安卓:layout_width="125dp"
                安卓:layout_height="30dp"
                安卓:layout_below="@+id/viewDivider"
                />

共 (4) 个答案

  1. # 1 楼答案

    你必须把ripple添加到你的背景中

    假设您有这样一个ripple文件,文件名为drawable中的ripple

    <?xml version="1.0" encoding="utf-8"?>
    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="#9D9D9D">
        <item>
            <shape android:shape="rectangle">
                <solid android:color="#9D9D9D" />
                <corners android:radius="20dp" />
            </shape>
        </item>
    
        <item android:drawable="@drawable/drawable_all_rows" />
    </ripple>
    

    ***第一个item给你的componentbutton添加效果,第二个item表示你没有点击button

    drawable_all_rows

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
        <solid android:color="#ffffff" />
        <corners android:radius="20dp" />
    
    </shape>
    

    然后你把它添加到你的relative layout背景中,如下所示:

  2. # 2 楼答案

    我只用一行

    android:background="?selectableItemBackground"
    
  3. # 3 楼答案

    只需在RelativeLayout中添加3行代码

    android:clickable="true"
    android:focusable="true"
    android:foreground="?selectableItemBackground"
    
  4. # 4 楼答案

    <?xml version="1.0" encoding="utf-8"?>
    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="@color/colorPrimaryDark">
        <item android:id="@android:id/mask">
            <shape android:shape="rectangle">
                <solid android:color="@color/colorPrimaryDark" />
                <corners android:radius="@dimen/_4sdp" />
            </shape>
        </item>
    
    <item android:id="@android:id/background">
        <shape android:shape="rectangle">
            <gradient
                android:angle="90"
                android:endColor="@color/blue"
                android:startColor="@color/colorPrimary"
                android:type="linear" />
            <corners android:radius="@dimen/_4sdp" />
        </shape>
    </item>