【问题标题】:How can I center a relatively sized element in an Android app?如何在 Android 应用中将相对大小的元素居中?
【发布时间】:2023-03-08 22:41:01
【问题描述】:

如果我正在使用:

<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 element center android-relativelayout


    【解决方案1】:

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

    【讨论】:

    • android:gravity="center" 谢谢!!!这非常有效,我知道这是我所缺少的一些简单的东西。我没有意识到重力会被应用到父母身上。我对孩子有偏见。
    【解决方案2】:

    根据您的要求,您的 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>
    

    【讨论】:

    • 谢谢你 ksarmalkar!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2013-09-19
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 2017-05-13
    相关资源
    最近更新 更多