簡體   English   中英

如何在Android應用程序中居中相對大小的元素?

[英]How can I center a relatively sized element in an Android app?

如果我正在使用:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:weightSum="1.0"
    android:background="@drawable/mainpgbg">
<RelativeLayout
    android:layout_width="0dip"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_weight="0.50"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_gravity="center_horizontal">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Log In"
            android:textSize="24sp"
            android:id="@+id/logInBtn"
            android:layout_gravity="center_vertical"
            android:layout_weight="0.50"
            android:background="@drawable/round"
            android:padding="20dp"
            android:layout_marginBottom="128dp"
            android:layout_alignParentBottom="true" />
</RelativeLayout>
</LinearLayout>

我預計RelativeLayout將在屏幕上居中顯示,但似乎您需要RelativeLayout來使元素居中,但是您需要LinearLayout來創建相對大小的元素。

我該怎么辦? 將相對大小的元素居中?

android:gravity="center"添加到LinearLayout的xml中,以使其子項居中(其中的任何內容):

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

根據您的要求,您的xml是正確的,您也可以嘗試使用TableLayout, http: //www.mkyong.com/android/android-tablelayout-example/

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<!-- 2 columns -->
<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dip" >

    <TextView
        android:id="@+id/textView1"
        android:text="Column 1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:text="Column 2" />
</TableRow>

<!-- edittext span 2 column -->
<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dip" >

    <EditText
        android:id="@+id/editText1"
        android:layout_span="2"
        android:text="Column 1 &amp; 2" />
</TableRow>

<!-- just draw a red line -->
<View
    android:layout_height="2dip"
    android:background="#FF0000" />

<!-- 3 columns -->
<TableRow
    android:id="@+id/tableRow3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dip" >

    <TextView
        android:id="@+id/textView2"
        android:text="Column 1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button2"
        android:text="Column 2" />

    <Button
        android:id="@+id/button3"
        android:text="Column 3" />
</TableRow>

<!-- display this button in 3rd column via layout_column(zero based) -->
<TableRow
    android:id="@+id/tableRow4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dip" >

    <Button
        android:id="@+id/button4"
        android:layout_column="2"
        android:text="Column 3" />
</TableRow>

<!-- display this button in 2nd column via layout_column(zero based) -->
<TableRow
    android:id="@+id/tableRow5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dip" >

    <Button
        android:id="@+id/button5"
        android:layout_column="1"
        android:text="Column 2" />
</TableRow>

暫無
暫無

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

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