【问题标题】:gravity attribute on android xml not workingandroid xml上的重力属性不起作用
【发布时间】:2014-06-30 10:59:48
【问题描述】:

我刚开始 android 编程。我一直在RelativeLayout 中编写xml,发现gravity 属性不起作用。布局中的每个元素都相互重叠。我知道可能有更好的方法来进行定位,但我很想知道,我做的不对是什么?请帮帮我

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
            <TextView 
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:text="@string/message"
                 android:gravity="start"
            />

           <Button  
              android:id="@+id/btnclose"
              android:textColor="#ffffff"
              android:background="#780956"
              android:textSize="18sp"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="close"
              android:text="close"
              android:gravity="bottom"
        />
    <Button  
        android:id="@+id/btnclick"
        android:textColor="#ffffff"
        android:background="#123456"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="close"
        android:text="click"
        android:gravity="center"
        />
</RelativeLayout>

【问题讨论】:

    标签: android xml gravity android-relativelayout


    【解决方案1】:

    不只是使用重力

    它们有两种类型 重力,布局重力

    一个会影响所有元素,而另一个不会

    两个都试试..

    【讨论】:

      【解决方案2】:

      当您使用 RelativeLayout 时,定位视图的正确方法是使用以下属性:

      1. android:centerInParent
      2. android:layout_toLeftOf
      3. android:layout_toRightOf
      4. android:layoutBelow

      阅读更多关于Positing Views的信息。

      【讨论】:

        【解决方案3】:

        在使用 RelativeLayout 时,请以这种方式设置属性:

            <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
        
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="message" />
        
            <Button
                android:id="@+id/btnclose"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:background="#780956"
                android:onClick="close"
                android:text="close"
                android:textColor="#ffffff"
                android:textSize="18sp" />
        
            <Button
                android:id="@+id/btnclick"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#123456"
                android:layout_centerInParent="true"
                android:onClick="close"
                android:text="click"
                android:textColor="#ffffff"
                android:textSize="18sp" />
        
        
        </RelativeLayout>
        

        【讨论】:

          猜你喜欢
          • 2015-02-03
          • 2013-11-28
          • 2021-12-30
          • 1970-01-01
          • 2013-09-01
          • 2017-03-04
          • 1970-01-01
          • 2017-02-02
          • 2012-07-10
          相关资源
          最近更新 更多