Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
80 views
in Technique[技术] by (71.8m points)

android - Button is not showing on View

I have a View in my activity where it contains one image, a text and a button... enter image description here

So as you can see my button is showing there but its not visible... It is going behind, I have tried to test it by marginTop attribute and if I move it down from the view then its visible...

I don't know how to do this but with the TextView when I use background then it changes the background color & works fine.. But I want to use Button instead of TextView

How can I do this or what I will have to do to bring the button front?

Below is my xml file code

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Dashboard">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <View
                android:id="@+id/view"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:background="@drawable/dashboard_layout"
                android:elevation="6dp"
                android:transitionName="bg_anim"
                android:layout_alignParentTop="true"
     />
    
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:elevation="6dp"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    <!--This is the button-->
<!--Removed            <Button
                android:id="@+id/tv_welcome"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="32dp"
                android:backgroundTint="@color/black"
                android:elevation="6dp"
               android:text="Hello,"
                android:textColor="#fff"
                android:textSize="35sp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/toolbar" />
    -->
<--added newly-->
       <Button
            android:id="@+id/tv_welcome"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:backgroundTint="@color/black"
            android:elevation="6dp"
            android:text="Hello,"
            android:textColor="#fff"
            android:textSize="35sp"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/toolbar"/>

            <TextView
                android:id="@+id/tv_username"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:elevation="6dp"
                android:text="Username"
                android:textColor="#B2FFFFFF"
                android:textSize="22sp"
                android:transitionName="user_anim"
                app:layout_constraintStart_toStartOf="@+id/tv_welcome"
                app:layout_constraintTop_toTopOf="@+id/guideline3" />
    
            <ImageView
                android:id="@+id/iv_display_image"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_marginTop="10dp"
    
    
                android:elevation="6dp"
                android:padding="8dp"
                android:transitionName="profile_anim"
                android:layout_centerHorizontal="true"
                app:layout_constraintDimensionRatio="h,1:1"
    
                app:srcCompat="@drawable/ic_undraw_male_avatar" />
    
            <TextView
                android:id="@+id/tv_dashboard"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="48dp"
                android:layout_marginTop="16dp"
                android:text="Your Dashboard"
                android:textColor="#ff1122"
                android:textSize="20sp"
                android:layout_below="@+id/view"/>
        </RelativeLayout>
    </androidx.core.widget.NestedScrollView>

After changing some code... enter image description here

question from:https://stackoverflow.com/questions/66061952/button-is-not-showing-on-view

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are using RelativeLayout as parent and adding constraints to Button which only works when the parent is ConstraintLayout, to achieve same in RelativeLayout try android:layout_alignParentStart="true" and android:layout_below="@+id/toolbar" as in

<Button
     android:id="@+id/tv_welcome"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginStart="32dp"
     android:backgroundTint="@color/black"
     android:elevation="6dp"
     android:text="Hello,"
     android:textColor="#fff"
     android:textSize="35sp"
     android:layout_alignParentStart="true"
     android:layout_below="@+id/toolbar" />

Also remove elevation on View as it is just setting background

 <View
     android:id="@+id/view"
     android:layout_width="match_parent"
     android:layout_height="250dp"
     android:background="@drawable/dashboard_layout"
     android:transitionName="bg_anim"
     android:layout_alignParentTop="true"/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...