簡體   English   中英

文字未在中心對齊

[英]Text not align in center

我想在中心的按鈕中設置文本,但它不起作用。 有沒有其他方法來編寫代碼。 我希望上半屏空,下半部分有4個按鈕。

碼-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/back"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_weight="4" >

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_weight="4" >

    <Button
            android:id="@+id/intro"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@null"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="Introduction" />

    <Button
            android:id="@+id/typ"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@null"
            android:layout_weight="1"
            android:text="Types" />

    <Button
            android:id="@+id/app"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@null"
            android:text="Application" />

    <Button
            android:id="@+id/ben"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center_vertical|center_horizontal"
            android:background="@null"
            android:layout_weight="1"
            android:text="Benefits" />

    </LinearLayout>

</LinearLayout>

圖片-

在此輸入圖像描述

由於這些按鈕的寬度設置為match_parent設置layout_gravity不會有任何影響。 你需要像這樣設置gravity

<Button
        android:id="@+id/intro"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:gravity="center"
        android:layout_weight="1"
        android:text="Introduction" />

<Button
        android:id="@+id/typ"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@null"
        android:layout_weight="1"
        android:text="Types" />

<Button
        android:id="@+id/app"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1"
        android:background="@null"
        android:text="Application" />

<Button
        android:id="@+id/ben"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@null"
        android:layout_weight="1"
        android:text="Benefits" />

並且還嘗試運行您的應用程序並測試有時IDE以這種方式顯示文本,但它實際上很好地居中。

這是我的食譜:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/back"
    android:orientation="vertical"
    >
    <View
        android:id="@+id/top_space_waster"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:background="@null"
        android:gravity="center"
        android:layout_weight="4"
    />
    <Button
        android:id="@+id/intro"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:background="@null"
        android:gravity="center"
        android:layout_weight="1"
        android:text="Introduction"
    />
    <Button
        android:id="@+id/typ"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:background="@null"
        android:layout_weight="1"
        android:text="Types"
    />
    <Button
        android:id="@+id/app"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:layout_weight="1"
        android:background="@null"
        android:text="Application"
    />
    <Button
        android:id="@+id/ben"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:background="@null"
        android:layout_weight="1"
        android:text="Benefits"
    />
</LinearLayout>

注意layout_height="0dp" ,權重需要工作。
另請注意,占用屏幕上半部分的View占用空間。

暫無
暫無

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

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