在分层布局的Andr​​oid与中心对齐 [英] Aligning with center in android with hierarchical Layouts

查看:124
本文介绍了在分层布局的Andr​​oid与中心对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不使用code。与中央保持一致?
什么我必须做修复它?

 < AbsoluteLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_gravity =中心
    机器人:ID =@ + ID /父
    机器人:layout_height =match_parent
    机器人:layout_width =match_parent
    机器人:背景=@色/蓝色>< RelativeLayout的
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =中心
    机器人:layout_width =WRAP_CONTENT>
    <按钮
        机器人:layout_gravity =中心
        机器人:ID =@ + ID / Question01
        机器人:文字=12 + 23
        机器人:重力=center_vertical | CENTER_HORIZONTAL
        机器人:layout_height =70像素
        机器人:行=1
        机器人:TEXTSIZE =40像素
        机器人:layout_alignWithParentIfMissing =真正的机器人:背景=@绘制/ orange_button机器人:layout_margin =5像素的android:layout_width =230px机器人:文字颜色=@色/蓝色/>
< / RelativeLayout的>
< / AbsoluteLayout>


解决方案

好了,的android:layout_gravity 是不是子视图定位,它是关于在当前视图中的定位其父。尝试使用安卓重力 AbsoluteLayout (也可能要切换到的FrameLayout 而不是Abdolute)。对于 RelativeLayout的,使用的android:layout_centerInParent =真正的按钮

 < AbsoluteLayout
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:比重=中心
机器人:ID =@ + ID /父
机器人:layout_height =match_parent
机器人:layout_width =match_parent
机器人:背景=@色/蓝色>
< RelativeLayout的
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_width =WRAP_CONTENT>
<按钮
    机器人:layout_gravity =中心
    机器人:ID =@ + ID / Question01
    机器人:文字=12 + 23
    机器人:重力=center_vertical | CENTER_HORIZONTAL
    机器人:layout_height =70像素
    机器人:行=1
    机器人:TEXTSIZE =40像素
    机器人:layout_alignWithParentIfMissing =真
    机器人:背景=@绘制/ orange_button
    机器人:layout_margin =5像素的android:layout_width =230px
    机器人:文字颜色=@色/蓝
    机器人:layout_centerInParent =真/>
< / RelativeLayout的>
< / AbsoluteLayout>

Why doesn't the following code align with the center? What do I have to do to fix it?

<AbsoluteLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_gravity="center" 
    android:id="@+id/parent" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:background="@color/blue">

<RelativeLayout
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_width="wrap_content">


    <Button 
        android:layout_gravity="center" 
        android:id="@+id/Question01" 
        android:text="12 + 23" 
        android:gravity="center_vertical|center_horizontal"
        android:layout_height="70px" 
        android:lines="1" 
        android:textSize="40px" 
        android:layout_alignWithParentIfMissing="true" android:background="@drawable/orange_button" android:layout_margin="5px" android:layout_width="230px" android:textColor="@color/blue"/>


</RelativeLayout>
</AbsoluteLayout>

解决方案

Well, android:layout_gravity is not about positioning of child views, it's about positioning of current view within its parent. Try using android:gravity for AbsoluteLayout (also you may want to switch to FrameLayout instead of Abdolute). For RelativeLayout, use android:layout_centerInParent="true" on the button.

<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center" 
android:id="@+id/parent" 
android:layout_height="match_parent" 
android:layout_width="match_parent" 
android:background="@color/blue">
<RelativeLayout
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content">
<Button 
    android:layout_gravity="center" 
    android:id="@+id/Question01" 
    android:text="12 + 23" 
    android:gravity="center_vertical|center_horizontal"
    android:layout_height="70px" 
    android:lines="1" 
    android:textSize="40px" 
    android:layout_alignWithParentIfMissing="true"
    android:background="@drawable/orange_button" 
    android:layout_margin="5px" android:layout_width="230px" 
    android:textColor="@color/blue"
    android:layout_centerInParent="true"/>
</RelativeLayout>
</AbsoluteLayout>

这篇关于在分层布局的Andr​​oid与中心对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆