【问题标题】:Center This Android Layout?将此 Android 布局居中?
【发布时间】:2012-03-02 00:38:52
【问题描述】:

我这辈子都不知道如何让我的盒子在水平和垂直方向上都居中。

这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/dragon"
    android:orientation="vertical" >
        
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/playerinfosquare"
        android:orientation="vertical">

        <TextView
            android:id="@+id/spellinfotitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="10px"
            android:text="Spellventory"
            android:textAppearance="?android:attr/textAppearanceLarge" />
        
        <TextView
            android:id="@+id/spell1info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="10px"
            android:text="No Spell Equipped"
            android:textAppearance="?android:attr/textAppearanceMedium" />
        
        <TextView
            android:id="@+id/spell2info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="10px"
            android:text="No Spell Equipped"
            android:textAppearance="?android:attr/textAppearanceMedium" />
        
        <TextView
            android:id="@+id/spell3info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="10px"
            android:text="No Spell Equipped"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    
    </LinearLayout>

</LinearLayout>

帮助?

你在开玩笑吗?堆栈溢出认为它比我更清楚,并说我没有足够的内容来解释我的代码部分。好吧,给你!这就是你拥有并不总是有意义的任意规则所得到的结果。

【问题讨论】:

  • 这条规则很重要,因为那里有很多只问代码的问题。对人好点! :D

标签: android xml layout


【解决方案1】:

将第二个 LinearLayout 放入 RelativeLayout。然后,您可以通过设置来更轻松地居中

android:layout_centerInParent="true"

在第二个 LinearLayout 上

【讨论】:

    【解决方案2】:

    您需要在您的第一个 LinearLayout 上设置 android:gravity="center"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      • 2012-10-25
      • 1970-01-01
      • 1970-01-01
      • 2012-04-10
      相关资源
      最近更新 更多