简体   繁体   English

Android:如何将textview放在按钮前面

[英]Android: How to put a textview in front of a button

I want to put a textview in front of a button, I couldn't do it, what I've tried: 我想将textview放在按钮前面,但我做不到,但我无法这样做:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <TextView
            android:id="@+id/text_timer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:text="10"
            android:textColor="@android:color/white" />

        <Button
            android:id="@+id/button_alert"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/alert" />
    </RelativeLayout>

I'd like to do it because I want the ripple effect when I click in the button, in the same time I want that show a counter in textview 我想这样做是因为我希望在单击按钮时产生涟漪效应,同时又希望在textview中显示一个计数器

Can anybody help me? 有谁能够帮助我? Thanks! 谢谢!

将此行添加到Button的XML中:

android:layout_toRightOf="@id/text_timer"

You can change to FrameLayout 您可以更改为FrameLayout

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">

<Button
    android:id="@+id/button_alert"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/alert" />

<TextView
    android:id="@+id/text_timer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:layout_gravity="right|end|center_vertical"
    android:text="10"
    android:textColor="@android:color/white" />
</FrameLayout>

Or easy move TextView after insert Button ^^ 或在插入Button ^^之后轻松移动TextView

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
xmlns:android="http://schemas.android.com/apk/res/android">

<Button
    android:id="@+id/button_alert"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Aleart" />

<TextView
    android:id="@+id/text_timer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:text="10"
    android:textColor="@android:color/white" />
</RelativeLayout>

Use android:elevation on the TextView to put it in front of the Button . TextView上使用android:elevation将其放在Button前面。 In this way you keep the effect when button is clicked. 这样,您可以在单击按钮时保持效果。

    <Button
            android:id="@+id/button"
            android:layout_width="@dimen/button_width"
            android:layout_height="@dimen/button_height"
            android:background="@color/colorButton"/>

    <TextView
            android:layout_width="@dimen/button_width"
            android:layout_height="@dimen/button_height"
            android:gravity="start|center"
            android:text="This is a button"
            android:elevation="10dp"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM