IT干货网

Android 文本不会居中对齐

zlslch 2023年05月26日 编程设计 5 0

我有一个 TextView 和 3 个 Buttons 位于 RelativeLayout 的中心,它们三个中的文本都是左对齐的父中心作为最左边的线。

我试过更改 TextViewgravityButtons 和布局,但没有任何效果,我很困惑,因为我以前从未见过 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> 

enter image description here


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

java - 如何在android中的数据库上更改数据时得到通知