簡體   English   中英

如何在線性布局中居中文本視圖

[英]How to center a textview inside a linearlayout

我有以下布局,我想讓 textview 出現在視圖的中心和中間。 我怎樣才能做到這一點?

在圖形視圖中查看布局時,我嘗試調整各種重力屬性,但似乎沒有任何改變。 我想要中心的 textview,我已經完成了但在視圖的一半。

謝謝馬特。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/carefreebgscaledalphajpg" >



    <TextView
        android:id="@+id/textviewdatefornocallspage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="You have no calls for "
        android:textSize="25sp" />

</LinearLayout>

將重力設置為你的 linearLayout 標簽的中心:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     android:gravity="center"
    android:background="@drawable/carefreebgscaledalphajpg" >

或使用這樣的RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/carefreebgscaledalphajpg" >



    <TextView
        android:id="@+id/textviewdatefornocallspage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="You have no calls for "
        android:textSize="25sp" />

</RelativeLayout>

試試這個

簡單我嘗試了很多答案,但所有答案都不起作用......最后這種方法對我有用

                     <LinearLayout
                        android:layout_below="@+id/iv_divider"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:layout_marginTop="@dimen/_10dp">
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="@string/mycredits"
                            android:textColor="@color/colorGray"
                            android:gravity="center"
                            android:textSize="@dimen/_25dp" />
                    </LinearLayout>

在線性布局內為 TextView 設置android:gravity="center"屬性,並將文本視圖的高度和寬度保持為wrap_content

這個解決方案對我有用。

    <TextView
...
    android:layout_gravity="center"
....
/>

android:gravity="center_horizontal"添加到您的LinearLayout並將您的TextViewlayout_width更改為android:layout_width="wrap_content"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:background="@drawable/carefreebgscaledalphajpg" >



    <TextView
        android:id="@+id/textviewdatefornocallspage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="You have no calls for "
        android:textSize="25sp" />

</LinearLayout>

將 textview 的寬度設置為wrap_content ,並將 LinearLayout 設置為

android:gravity="center_horizontal"

如果在線性布局中您的方向垂直,您可以通過android:layout_gravity="center"將 textview 放在“水平android:layout_gravity="center" 要垂直居中 textview,您需要將 textview 的 layout_height 設置為 match_parent 並將 android:gravity 設置為“center”。 如果你想在這個布局中使用多個視圖,你應該使用 RelativeLayout 並設置android:layout_centerInParent="true"

聽起來你想要android:layout_gravity為 TextView

請參閱文檔: developer.android.com

在你的線性布局中設置android:gravity="center"

其他答案都不適合我。 我不得不使用android:textAlignment="center"

我的布局如下

<LinearLayout...
    <RelativeLayout...
        <RelativeLayout....
            <TextView
                android:layout_centerInParent="true"
                android:textAlignment="center"
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".view.fragments.FragmentAncRecords">

    <TextView
        android:id="@+id/tvNoDataFound"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fontFamily="@font/poppins_regular"
        android:visibility="visible"
        android:gravity="center"
        android:text="@string/no_record_available"
        />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvAncRecords"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />



</LinearLayout>

將您的 texteView 居中於 RelativeLayout :

<?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=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    />

</RelativeLayout>

使用相對布局,它總是提供更准確和靈活的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/ic_launcher" />
</RelativeLayout>

暫無
暫無

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

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