关于Android中RelativeLayout下面的三个属性:layout_centerVertical,layout_centerInParent,layout_centerHorizontal

本文详细解析了Android中RelativeLayout布局的特定属性,特别是android:layout_centerVertical=true的作用,通过实例对比展示了该属性如何使控件在其父布局中实现垂直居中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天在看网上的大神写的新浪微博客户端的页面代码时,在一个relativelayout里面看见了


 android:layout_centerVertical="true" 这一段


于是乎这段代码所起的作用让我产生了兴趣,关注的焦点就是:这段代码所作用的结果。

当时我在看eclipse开发工机的xml的视图页面的时候,发现有了这句话之后,相应的RelativeLayout中间出现了一条绿线,我的直观感受就是这句话应该是作用于这个RelativeLayout中的控件的。

但是本着科学的态度,我又查了查,发现网上的资料很少,最后我使出了杀手锏:自己实践!

结果出乎我的意料:

其实这句话的作用是:让这个相对布局,处于它父控件的垂直方向的中心。并不是指定自己内部控件处于垂直方向的中心

之前看了一个大神些的文章:http://blog.youkuaiyun.com/softkexin/article/details/5933589 

这篇文章的就是讲RelativeLayout中的一些属性的作用的

最后又三句:”

android:layout_centerInParent="true"  --将控件置于父控件的中心位置
android:layout_centerHorizontal="true"  --将控件置于水平方向的中心位置
android:layout_centerVertical="true"  --将控件置于垂直方向的中心位置

这三句的后面两个的说明应该改为:

android:layout_centerHorizontal="true"  --将控件置于父控件水平方向的中心位置
android:layout_centerVertical="true"  --控件置于父控件垂直方向的中心位置

下面这是android:layout_centerVertical="true"效果截图:


对应的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        <span style="color:#ff0000;">android:layout_centerVertical="true"</span> >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="helloworld" />
    </RelativeLayout>

</RelativeLayout>
下面这是android:layout_centerHorizontal="true"效果截图:

对应的代码:

<span style="color:#333333;"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        </span><span style="color:#ff0000;">android:layout_centerHorizontal="true" </span><span style="color:#333333;">>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="helloworld" />
    </RelativeLayout>

</RelativeLayout></span>

至于android:layout_centerInParent="true",这个属性其实就相当于把上面两个属性综合了一下,跟同时指定前两个为true是相同的效果。

还有一点需要补充:这些属性,只有在父控件为RelativeLayout时才会出现,也就是说,父控件是相对布局的时候子控件就会有这三个属性,

不管子控件是什么布局也好,什么TextView,Button也好,都会有这些属性。

而这时作为父控件的RelativeLayout本身,是不具有这些属性的。也就是说,一旦一个控件可以指定这三个属性,那么它应该就是某个RelativeLayout的子控件。当然,这只是我目前得出的结论,到底正确与否还不是很清楚,但是这都是目前我基于实践得出来的结论,至少在当前,可以作为参考。

    最后谈一点自己的感想:

            学习这个东西,真的是要多思考,这样才能触类旁通,还要有钻研的精神,实事求是的态度,我之所以会对这个属性的显示结果感到好奇,也有很大一部分原因,在于我不久前研究了android:gravity与android:layout_gravity的区别,gravity是作用于这个控件的子控件的;而layout_gravity则是作用于使用这个属性的控件本身的,它是指定让自己处于父控件的什么位置上(靠左,靠右,居中);当我看见android:layout_centerVertical=true这个属性出现在的控件是一个RelativeLayout中时,我的第一反应就是这个属性是作用在子控件上的,然后我又去视图编辑器里面看,发现有了这个属性相对布局本身中间多了一条绿色的横线。就主观来推测的话,至少我是认为,这个横线说明这个布局里面的控件排列遵循这条横线。这样就得出了有了这个属性,布局内部控件要在竖直方向上居中的结论,但是我想到:layout_gravity不是作用于子控件的,而是作用于自身的,指定自身相对于父控件的位置,这样的话,让我直接联想到的就是:带有"layout_"这样的属性,应该都是作用于自身才对,有的朋友可能要问了:为什么要作用于自身?

其实我想到的原因很简单,google的设计人员,跟我们一样,也喜欢简单易用的原则,那么如果带有"layout_"这样名字的属性,既有作用于子控件的,又有作用于自身的,那样的话,使用起来感觉很混乱,而且也不方便编程人员的记忆,从这个角度讲android布局本身的易用性也会打折扣,作为全世界最优秀的软件设计人员,不可能犯这么二的错误吧~

      所以我就抱着试试看的态度,进行了这两个属性的试验,结果果然跟自己的第一印象不同。也验证了我的另一个推测:带有"layout_"字样的属性,一般情况下都是作用于自身的。

    总结一下我一开始的话,学习这个东西真的要多思考(比如知道了android:gravity与android:layout_gravity的作用范围区别之后,看到带有"layout_"字样的属性的时候就要注意,与不带"layout_"的同名属性之间的区别是不是跟gravity的情况类似),还要有钻研精神(比如在网上找了很多的资料,但是没有一个讲的特别清楚的情况下,要做好自己调查的思想准备,因为这样才能真正掌握),实事求是的态度(比如我在这个例子中的表现,一开始就先入为主的感觉作用范围是什么,这样的推测的方式不能说不好,因为很多时候我们的推测也都是有依据的,很多时候也会做出正确的推测,但是之后要有一个实事求是的态度去看待,不能自己推测之后就主观臆断,不能怕麻烦,怕动手浪费时间,其实自己去求真的结果很可能是你的做法不仅没有浪费时间,而且还为以后节省了很多的时间)。

   好了,废话一大堆,希望这篇文章可以帮到一些也遇到同样问题的朋友~谢谢~~^o^



<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff2f2f2" tools:context="com.hik.netsdk.SimpleDemo.View.MainActivity"> <RelativeLayout android:id="@+id/ra_title" android:layout_width="match_parent" android:layout_height="44dp" android:background="@mipmap/title_bg"> <TextView android:id="@+id/titlename" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_gravity="center" android:text="易丰视频监控" android:textColor="@android:color/white" android:textSize="18sp" /> <ImageButton android:id="@+id/back" android:layout_width="40dp" android:layout_height="match_parent" android:background="@null" android:paddingLeft="10dp" android:contentDescription="视频插件" android:src="@mipmap/img_back" /> <ImageButton android:id="@+id/ib_rotate" android:layout_width="50dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:onClick="changeScreen" android:layout_marginRight="6dp" android:scaleType="centerInside" android:background="@drawable/gps_select" android:contentDescription="视频插件" android:src="@mipmap/ic_size_sel" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_control" android:layout_width="match_parent" android:layout_height="266dp" android:layout_alignParentBottom="true" > <LinearLayout android:id="@+id/ll_center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerInParent="true" android:background="@mipmap/ycjk_yp" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb1" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb2" /> <ImageButton android:visibility="gone" android:id="@+id/left_up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@null" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb3" /> </LinearLayout> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb4" /> </LinearLayout> <ImageButton android:id="@+id/ptz_top_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@drawable/nnew_video_up" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb1" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb3" /> <ImageButton android:visibility="gone" android:id="@+id/right_up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb2" /> </LinearLayout> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb4" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" > <ImageButton android:id="@+id/ptz_left_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@drawable/nnew_video_left" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_zj" /> <ImageButton android:id="@+id/ptz_right_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:contentDescription="视频插件" android:background="@drawable/nnew_video_right" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb4" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb2" /> <ImageButton android:visibility="gone" android:id="@+id/left_down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb3" /> </LinearLayout> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb1" /> </LinearLayout> <ImageButton android:id="@+id/ptz_bottom_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@drawable/nnew_video_down" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb4" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:contentDescription="视频插件" android:background="@mipmap/ycjk_kb3" /> <ImageButton android:visibility="gone" android:id="@+id/right_down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@null" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@mipmap/ycjk_kb2" /> </LinearLayout> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb1" /> </LinearLayout> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toLeftOf="@id/ll_center" android:gravity="center" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> <ImageButton android:id="@+id/focus_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@drawable/video_more1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:textSize="16dp" android:text="焦距 +" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical" > <ImageButton android:id="@+id/guangquan_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@drawable/video_more3" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:text="光圈 +" android:contentDescription="视频插件" android:textSize="16dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical" > <ImageButton android:id="@+id/zoom_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@drawable/video_more5" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:text="变倍 +" android:textSize="16dp" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toRightOf="@id/ll_center" android:gravity="center" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical" > <ImageButton android:id="@+id/foucus_reduce" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@drawable/video_more2" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:text="焦距 -" android:textSize="16dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical" > <ImageButton android:id="@+id/guangquan_reduce" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@drawable/video_more4" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:text="光圈 -" android:contentDescription="视频插件" android:textSize="16dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical" > <ImageButton android:id="@+id/zoom_reduce" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@drawable/video_more6" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:text="变倍 -" android:textSize="16dp" /> </LinearLayout> </LinearLayout> </RelativeLayout> <RelativeLayout android:layout_above="@id/rl_control" android:layout_below="@id/ra_title" android:layout_width="match_parent" android:layout_height="match_parent" > <SurfaceView android:id="@+id/realplay_sv" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" /> <ImageButton android:id="@+id/ib_rotate2" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignParentLeft="true" android:layout_marginLeft="3dp" android:background="@color/green" android:onClick="changeScreen" android:contentDescription="视频插件" android:src="@mipmap/img_systems_close" /> <ProgressBar android:id="@+id/liveProgressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> <LinearLayout android:id="@+id/ll_hc" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" > <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" > <ImageButton android:id="@+id/ptz_top_btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@mipmap/h_up" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" > <ImageButton android:id="@+id/ptz_bottom_btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@mipmap/h_down" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" > <ImageButton android:id="@+id/ptz_left_btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@mipmap/h_left" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" > <ImageButton android:id="@+id/ptz_right_btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@mipmap/h_right" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" > <ImageButton android:id="@+id/focus_add2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@drawable/video_more1" /> </LinearLayout> <LinearLayout android:gravity="center" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > <ImageButton android:id="@+id/foucus_reduce2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@drawable/video_more2" /> </LinearLayout> <LinearLayout android:gravity="center" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > <ImageButton android:id="@+id/zoom_add2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@drawable/video_more5" /> </LinearLayout> <LinearLayout android:gravity="center" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > <ImageButton android:id="@+id/zoom_reduce2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@drawable/video_more6" /> </LinearLayout> </LinearLayout> </RelativeLayout> </RelativeLayout> 依据上述代码解决报错 <ImageButton>: Touch target size too small <ImageButton>: Touch target size too small <ImageButton>: Touch target size too small <ImageButton>: Touch target size too small <ImageButton>: Touch target size too small
最新发布
06-24
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/chat_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="#000000" android:keepScreenOn="true" android:layout_height="match_parent"> <LinearLayout android:id="@+id/rl_title" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content" android:background="@color/black_alpha_5"> <View android:id="@+id/statusBar" android:layout_width="match_parent" android:layout_height="0dp" android:background="#000000"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="?actionBarSize"> <LinearLayout android:layout_alignParentLeft="true" android:id="@+id/exit" android:visibility="gone" android:layout_width="?actionBarSize" android:gravity="center" android:layout_height="?actionBarSize"> <TextView android:layout_width="wrap_content" android:text="退出" android:textColor="#FFFFFF" android:layout_height="wrap_content"/> </LinearLayout> <TextView android:id="@+id/memberName" android:layout_width="wrap_content" android:text="音频会议" android:textColor="#FFFFFF" android:layout_centerInParent="true" android:layout_height="wrap_content"/> <LinearLayout android:id="@+id/memberMenu" android:layout_alignParentRight="true" android:layout_width="44dp" android:gravity="center" android:visibility="gone" android:layout_centerVertical="true" android:layout_height="44dp"> <ImageView android:layout_alignParentRight="true" android:layout_width="24dp" android:scaleType="fitXY" android:src="@mipmap/icon_menu_more_member" android:layout_height="24dp"/> </LinearLayout> </RelativeLayout> </LinearLayout> <FrameLayout android:layout_below="@id/rl_title" android:layout_width="match_parent" android:layout_height="match_parent"> <!--会议View--> <FrameLayout android:visibility="gone" android:id="@+id/admin_surface_container" android:layout_width="match_parent" android:layout_height="match_parent" /> <RelativeLayout android:layout_width="match_parent" android:background="@color/dk_color_4c00C9F4" android:layout_height="match_parent"> <com.google.android.flexbox.FlexboxLayout xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/username_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="16dp" android:paddingBottom="16dp" android:paddingStart="8dp" android:layout_centerHorizontal="true" android:layout_centerVertical="true" app:flexWrap="wrap" app:flexDirection="row" app:alignItems="flex_start"> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_circle" android:gravity="center" android:textColor="@android:color/white" android:textSize="14sp" android:layout_marginStart="18dp" android:text="陈倩蓓" android:padding="4dp" /> </com.google.android.flexbox.FlexboxLayout> <!--底部按钮--> <LinearLayout android:id="@+id/bottomLayout" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:orientation="vertical" android:layout_gravity="bottom|right" android:layout_height="wrap_content"> <TextView android:id="@+id/time2" android:layout_width="wrap_content" android:textColor="#FFFFFF" android:layout_gravity="center" android:layout_marginBottom="16dp" android:layout_height="wrap_content"/> <LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:gravity="center" android:layout_marginBottom="40dp" android:layout_height="wrap_content"> <ImageView android:id="@+id/mute" android:layout_width="wrap_content" android:src="@drawable/rc_voip_mute_selector" android:layout_height="wrap_content"/> <ImageView android:id="@+id/hangup" android:layout_marginLeft="44dp" android:layout_marginRight="44dp" android:layout_width="wrap_content" android:src="@mipmap/rc_voip_hang_up" android:background="@null" android:layout_height="wrap_content"/> <ImageView android:id="@+id/audio" android:layout_width="wrap_content" android:src="@drawable/rc_voip_speaker_selector" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout> </RelativeLayout> </FrameLayout> </RelativeLayout> 如何让"陈倩蓓"所在的组件在父组件中水平居中
05-31
### Android 布局属性 `layout_centerHorizontal` 的使用示例与问题解决方案 #### 使用场景说明 在 Android 开发中,`layout_centerHorizontal` 是 RelativeLayout 中的一个重要属性,用于将子视图水平居中放置在其父容器内。此属性通常与其他尺寸和定位属性配合使用,以实现精确的布局效果。 以下是基于提供的 XML 配置实例化解释: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/tv_num" android:text="@string/app_name" android:layout_width="60dp" android:layout_height="50dp" android:layout_centerHorizontal="true" /> </RelativeLayout> ``` 上述代码片段展示了如何通过设置 `layout_centerHorizontal="true"` 将 TextView 控件水平居中于其父级 RelativeLayout 容器[^1]。 #### 属性对比分析 为了更好地理解 `layout_centerHorizontal` 的作用及其差异性,可以将其与其他类似的属性进行比较: - **`layout_gravity`**:属性属于 LinearLayout 或其他支持 Gravity 特性的布局管理器中的配置项。它定义的是当前 ViewGroup 内部子组件的整体排列方式。例如,在一个垂直方向上的线性布局里应用 `layout_gravity="center_horizontal"` 可使所有子元素沿横向中心对齐[^2]。 - **`gravity`**: 主要影响内部文本或其他内容相对于自身边界框的位置调整。比如当我们在 TextView 上指定 `android:gravity="center_horizontal"` 时,则意味着该控件内的文字会按照设定好的规则向中间靠拢[^3]。 - **`layout_centerInParent`**, **`layout_centerVertical`** 和 **`layout_centerHorizontal`**: 这些都是专属于 RelativeLayout 的特性参数。其中前者能够一次性完成整个区域范围内的完全重心定位;而另外两者分别专注于单一维度——即要么仅限横轴要么只针对纵坐标系下的对象安置操作[^4]。 #### 解决常见问题的方法论建议 如果遇到有关 `layout_centerHorizontal` 不生效或者表现异常的情况,请考虑以下几个方面的原因排查路径: 1. 确认所使用的外层容器确实为 RelativeLayout 类型; 2. 检查是否有冲突性质更强别的约束条件覆盖掉了原本期望的效果(如 margin 设置过大可能间接改变实际渲染位置); 3. 如果涉及动态加载数据更新界面逻辑的话还需要留意时机把握是否恰当以及是否存在潜在竞态风险等问题存在. ```java // 动态修改 LayoutParams 实现相同功能的例子 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.CENTER_HORIZONTAL); // 添加 center horizontal rule tvNum.setLayoutParams(params); ``` 以上 Java 代码演示了怎样程序运行期间更改某个已有视图元件的相关属性从而达到预期目的之一种方法.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值