Add textview to layout?

前端 未结 4 538
孤城傲影
孤城傲影 2021-01-27 13:08

I cant put a textview above my buttons without having my buttons move...How can I fix this?

Been stuck on this for 4 hours...Which is sad because I\'m t

相关标签:
4条回答
  • 2021-01-27 13:10

    It will work in all size of devices but you have to take of text size of left TextView dynamically. TextView text size should be small according device density. hope it will help.!

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#010101"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.ruchir.circleswithmap.MainActivity" >
    
    <LinearLayout
        android:id="@+id/text_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/relativeLayout"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp" >
    
        <TextView
            android:id="@+id/timetext"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="20:00"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#fbfafa"
            android:textSize="50sp" />
    </LinearLayout>
    
    <TextView
        android:id="@+id/txt"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="50:00"
        android:gravity="center_vertical|right"
        android:layout_toLeftOf="@+id/relativeLayout"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#fbfafa"
        android:textSize="50sp" />
    
    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" >
    
        <ImageButton
            android:id="@+id/Blue"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/ic_launcher" />
    
        <ImageButton
            android:id="@+id/Green"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@+id/Blue"
            android:background="@drawable/ic_launcher" />
    
        <ImageButton
            android:id="@+id/Red"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignTop="@+id/Blue"
            android:layout_toEndOf="@+id/Blue"
            android:layout_toRightOf="@+id/Blue"
            android:background="@drawable/ic_launcher" />
    
        <ImageButton
            android:id="@+id/Purple"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignBottom="@+id/Green"
            android:layout_alignLeft="@+id/Red"
            android:layout_alignStart="@+id/Red"
            android:background="@drawable/ic_launcher" />
    </RelativeLayout>
    

    0 讨论(0)
  • 2021-01-27 13:11

    You can wrap the buttons inside a layout and then place a textview above it and tell the new layout to be below the text view

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.ruchir.circleswithmap.MainActivity">
    
    <TextView
        android:id="@+id/textview111"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/password_text_view"
        android:layout_alignParentTop="true"/>
    
    <RelativeLayout
        android:layout_below="@id/textview111"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
    
        <ImageButton
            android:id="@+id/Blue"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:background="@color/primaryColor" />
    
        <ImageButton
            android:id="@+id/Green"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignLeft="@+id/Blue"
            android:layout_alignStart="@+id/Blue"
            android:layout_below="@+id/Blue"
            android:background="@color/primaryColor" />
    
        <ImageButton
            android:id="@+id/Red"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignTop="@+id/Blue"
            android:layout_toEndOf="@+id/Blue"
            android:layout_toRightOf="@+id/Blue"
            android:background="@color/primaryColor" />
    
        <ImageButton
            android:id="@+id/Purple"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignBottom="@+id/Green"
            android:layout_alignLeft="@+id/Red"
            android:layout_alignStart="@+id/Red"
            android:background="@color/primaryColor" />
    </RelativeLayout>
    
    </RelativeLayout>
    

    and this should do the trick, hope it helps

    0 讨论(0)
  • 2021-01-27 13:18

    A simple and quick solution could be to use two layouts: keep the RelativeLayout and embed it in a vertical LinearLayout. Then you can just add the TextView to the LinearLayout before the RelativeLayout.

    Caution: using multiple layers of Layouts can hurt performance, so don't overdo it. Two layouts shouldn't hurt though.

    0 讨论(0)
  • 2021-01-27 13:22
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    tools:context="com.example.ruchir.circleswithmap.MainActivity"
                    android:id="@+id/layout">
    
        <LinearLayout android:layout_width="wrap_content"
                      android:id="@+id/text_container"
                      android:layout_centerInParent="true"
                      android:layout_alignParentTop="true"
                      android:layout_height="wrap_content">
            <TextView android:layout_width="match_parent"
                      android:text="pruebaskjahlkdjahslk"
                      android:layout_height="match_parent"/>
        </LinearLayout>
    
        <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_centerInParent="true"
                android:layout_height="wrap_content">
    
            <ImageButton
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:id="@+id/Blue"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"/>
    
            <ImageButton
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:id="@+id/Green"
                    android:layout_below="@+id/Blue"
                    android:layout_alignLeft="@+id/Blue"
                    android:layout_alignStart="@+id/Blue"/>
    
            <ImageButton
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:id="@+id/Red"
                    android:layout_alignTop="@+id/Blue"
                    android:layout_toRightOf="@+id/Blue"
                    android:layout_toEndOf="@+id/Blue"/>
    
            <ImageButton
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:id="@+id/Purple"
                    android:layout_alignBottom="@+id/Green"
                    android:layout_alignLeft="@+id/Red"
                    android:layout_alignStart="@+id/Red"/>
    
        </RelativeLayout>
    </RelativeLayout>
    

    Landscape orientation

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    tools:context="com.example.me.circleswithmap.MainActivity"
                    android:id="@+id/layout">
    
        <LinearLayout android:layout_width="wrap_content"
                      android:id="@+id/text_container"
                      android:layout_height="wrap_content"
                      android:layout_centerHorizontal="true">
            <TextView
                    android:layout_width="match_parent"
                    android:text="20:00"
                    android:layout_height="match_parent"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    />
        </LinearLayout>
    
        <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/relativeLayout"
                android:layout_below="@id/text_container"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true"
                >
    
            <ImageButton
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:id="@+id/Blue"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"/>
    
            <ImageButton
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:id="@+id/Green"
                    android:layout_below="@+id/Blue"
                    android:layout_alignLeft="@+id/Blue"
                    android:layout_alignStart="@+id/Blue"/>
    
            <ImageButton
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:id="@+id/Red"
                    android:layout_alignTop="@+id/Blue"
                    android:layout_toRightOf="@+id/Blue"
                    android:layout_toEndOf="@+id/Blue"/>
    
            <ImageButton
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:id="@+id/Purple"
                    android:layout_alignBottom="@+id/Green"
                    android:layout_alignLeft="@+id/Red"
                    android:layout_alignStart="@+id/Red"/>
    
        </RelativeLayout>
    
    </RelativeLayout>
    
    0 讨论(0)
提交回复
热议问题