簡體   English   中英

Android文字將無法對齊中心

[英]Android text will not align to center

我有一個TextView和3個位於RelativeLayout中心的Buttons ,並且所有這三個Buttons中的文本都是左對齊的,父中心為最左行。

我嘗試過更改TextViewButtons和布局的gravity ,但沒有任何效果,我感到困惑,因為我之前從未見過Android會像這樣對齊文本。

我希望Button文本在每個Button居中,頂部文本在居中對齊。

它的外觀圖像在這里: http : //i.stack.imgur.com/9lHEd.png

這是我的xml:

    <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" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_margin="20dp"
    android:gravity="center_horizontal"
    android:text="@string/greeting"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/startQuizButton"
    android:layout_width="280dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:onClick="@string/startQuiz"
    android:text="@string/start_quiz" />

<Button
    android:id="@+id/lessonsButton"
    android:layout_width="280dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/startQuizButton"
    android:layout_centerHorizontal="true"
    android:onClick="@string/startLesson"
    android:text="@string/lessonsString" />

<Button
    android:id="@+id/viewAllButton"
    android:layout_width="280dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/lessonsButton"
    android:layout_centerHorizontal="true"
    android:onClick="@string/showAll"
    android:text="@string/view_all" />

我包含了一個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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="Greetings" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Start Quiz" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Lessons" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="View all questions" />
    </LinearLayout>

</RelativeLayout>

在此輸入圖像描述

在按鈕選項卡中添加android:gravity =“ center” ...查看是否有效。

如果您想將某些文本居中,那么您也可以這樣:

    <TextView
    android:text="Quantity"
    android:textSize="34sp"
    android:textColor="#ffffff"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="sans-serif-light"
    android:layout_centerHorizontal="true"
    android:paddingTop="10dp"/>

在這里,我通過使用android:layout_centerHorizontal="true"其更改為horozontal,它將布爾值設置為true或false,將其設置為true,它將自動以所有設備為中心,希望對您有所幫助

您需要將布局的寬度和高度設置為fill_parent,一切都會正常進行

<TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:text="your text" />

暫無
暫無

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

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