【问题标题】:Android layout is off. need to fix positionAndroid 布局已关闭。需要固定位置
【发布时间】:2013-11-16 20:23:37
【问题描述】:

我希望右侧的布局与左侧的布局相匹配,其中包含“20”的文本框。左边的那个是我想要的位置,但右边的那个不会正确定位。我还想在 20 上方的右上角有一个小微调器

<?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:orientation="horizontal" >

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:gravity="center" >

    <TextView
        android:id="@+id/history1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="98dp"
        android:text="This is where life histroy goes" />

    <EditText
        android:id="@+id/playerLifeOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="49dp"
        android:gravity="center"
        android:inputType="number"
        android:minWidth="120dp"
        android:text="20"
        android:textSize="40dp" />

</RelativeLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >

   <Button
       android:id="@+id/badd1"
       android:layout_width="78dp"
       android:layout_height="wrap_content"
       android:text="+1" />

        <Button
            android:id="@+id/badd5"
           android:layout_width="78dp"
            android:layout_height="wrap_content"
            android:text="+5" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/bsub1"
            android:layout_width="78dp"
            android:layout_height="wrap_content"
            android:text="-1" />

        <Button
            android:id="@+id/bsub5"
            android:layout_width="78dp"
            android:layout_height="wrap_content"
            android:text="-5" />
    </LinearLayout>
</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@android:color/black" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="2"
    android:gravity="center" >

    <EditText
        android:id="@+id/playerLifeTwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:gravity="center"
        android:inputType="number"
        android:minWidth="120dp"
        android:text="20"
        android:textSize="40dp" >

        <requestFocus />
    </EditText>

 </LinearLayout>

 <LinearLayout
    android:layout_width="156dp"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/b2add1"
            android:layout_width="78dp"
            android:layout_height="wrap_content"
            android:text="+1" />

        <Button
            android:id="@+id/b2add5"
            android:layout_width="78dp"
            android:layout_height="wrap_content"
            android:text="+5" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/b2sub1"
           android:layout_width="78dp"
            android:layout_height="wrap_content"
            android:text="-1" />

        <Button
            android:id="@+id/b2sub5"
            android:layout_width="78dp"
            android:layout_height="wrap_content"
            android:text="-5" />

    </LinearLayout>
 </LinearLayout>
 </LinearLayout>

</LinearLayout>

【问题讨论】:

  • 你能分享一张图片吗?很容易理解你在找什么;)
  • 这就是你现在所拥有的。我想知道你想要什么?

标签: android layout textview android-relativelayout


【解决方案1】:

这对我有用。更改 #2 以匹配 #1 :

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2" >

        <EditText
            android:id="@+id/playerLifeTwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="49dp"
            android:gravity="center"
            android:inputType="number"
            android:minWidth="120dp"
            android:text="20"
            android:textSize="40dp" />
    </RelativeLayout>

【讨论】:

    【解决方案2】:

    如果您正在寻找类似下图的内容,您可以使用(修改后的)布局。

    <?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:orientation="horizontal" >
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:orientation="vertical" >
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="2"
                android:gravity="center" >
    
                <TextView
                    android:id="@+id/history1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="98dp"
                    android:text="This is where life histroy goes" />
    
                <EditText
                    android:id="@+id/playerLifeOne"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="49dp"
                    android:gravity="center"
                    android:inputType="number"
                    android:minWidth="120dp"
                    android:text="20"
                    android:textSize="40dp" />
            </RelativeLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:orientation="horizontal" >
    
                    <Button
                        android:id="@+id/badd1"
                        android:layout_width="78dp"
                        android:layout_height="wrap_content"
                        android:text="+1" />
    
                    <Button
                        android:id="@+id/badd5"
                        android:layout_width="78dp"
                        android:layout_height="wrap_content"
                        android:text="+5" />
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:orientation="horizontal" >
    
                    <Button
                        android:id="@+id/bsub1"
                        android:layout_width="78dp"
                        android:layout_height="wrap_content"
                        android:text="-1" />
    
                    <Button
                        android:id="@+id/bsub5"
                        android:layout_width="78dp"
                        android:layout_height="wrap_content"
                        android:text="-5" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@android:color/black" />
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:orientation="vertical" >
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="2"
                android:gravity="top" >
    
                <EditText
                    android:id="@+id/playerLifeTwo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="49dp"
                    android:gravity="center"
                    android:inputType="number"
                    android:minWidth="120dp"
                    android:text="20"
                    android:textSize="40dp" >
                    <requestFocus />
                    </EditText>
                    <Spinner 
                        android:id="@+id/spinner"
                        android:layout_alignParentTop="true"
                        android:layout_alignParentRight="true"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"/>
            </RelativeLayout>
    
            <LinearLayout
                android:layout_width="156dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:orientation="vertical" >
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:orientation="horizontal" >
    
                    <Button
                        android:id="@+id/b2add1"
                        android:layout_width="78dp"
                        android:layout_height="wrap_content"
                        android:text="+1" />
    
                    <Button
                        android:id="@+id/b2add5"
                        android:layout_width="78dp"
                        android:layout_height="wrap_content"
                        android:text="+5" />
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:orientation="horizontal" >
    
                    <Button
                        android:id="@+id/b2sub1"
                        android:layout_width="78dp"
                        android:layout_height="wrap_content"
                        android:text="-1" />
    
                    <Button
                        android:id="@+id/b2sub5"
                        android:layout_width="78dp"
                        android:layout_height="wrap_content"
                        android:text="-5" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-30
      • 1970-01-01
      • 2010-09-24
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2013-08-18
      • 1970-01-01
      相关资源
      最近更新 更多