简体   繁体   English

Android:RelativeLayout中的重力ImageView失败

[英]Android: gravity ImageView in RelativeLayout failed

I would like to align this ImageView in center (horizontal and vertical): 我想将ImageView center对齐(水平和垂直):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/titletab"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="@dimen/slidingtab_icon_header"
    android:gravity="center">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="@dimen/slidingtab_item_icon_size"
        android:layout_height="@dimen/slidingtab_item_icon_size"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_event_black_18dp"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"
        android:background="@color/sliding_tab_border_color"/>

</RelativeLayout>

If I don't add the line (the View ) after my ImageView it's perfect, it's work, but with this line the gravity doesn't work. 如果我没有在ImageView之后添加这条线(即View ),那是完美的,那是可行的,但是对于这条线, gravity不起作用。

Thanks by advance! 预先感谢!

You need to change some property and your view is perfect. 您需要更改一些属性,并且您的视图是完美的。 Just check below and you see I change gravity property in . 只需检查下面的内容,您就会看到我在中更改了重力属性。

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

    <ImageView
        android:id="@+id/icon"
        android:layout_width="@dimen/slidingtab_item_icon_size"
        android:layout_height="@dimen/slidingtab_item_icon_size"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"
        android:background="@color/sliding_tab_border_color"/>

</RelativeLayout>

You need to use 您需要使用

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

instead of 代替

android:layout_gravity="center_horizontal|center_vertical"

because This is works in LinearLayout . 因为这在LinearLayout中有效 But, for RelativeLayout you need to use above property for gravity set. 但是,对于RelativeLayout,您需要对重力集使用上述属性。

Try android:layout_centerInParent="true" property of Relativelayout and remove the gravity . 尝试使用Relativelayout android:layout_centerInParent="true"属性,并删除gravity

<ImageView
        android:id="@+id/icon"
        android:layout_width="@dimen/slidingtab_item_icon_size"
        android:layout_height="@dimen/slidingtab_item_icon_size"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_event_black_18dp"/>

You shoud use layout_alignBottom instead layout_alignParentBottom in View element. 您应该在View元素中使用layout_alignBottom而不是layout_alignParentBottom。

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_alignBottom="@+id/icon"
    android:background="@color/sliding_tab_border_color" />

Change your code with this . 更改您的代码与此。 It is working and i changed the icon to the one which was available with me, you can use your icon 它正在运行,我将图标更改为可以使用的图标,您可以使用您的图标

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/titletab"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="@dimen/slidingtab_icon_header">



    <ImageView
        android:id="@+id/icon"
        android:layout_width="@dimen/slidingtab_item_icon_size"
        android:layout_height="@dimen/slidingtab_item_icon_size"
        android:src="@drawable/abc_ic_ab_back_mtrl_am_alpha"
        android:layout_gravity="center_horizontal"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/li">

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_alignParentBottom="true"/>
    </LinearLayout>

</RelativeLayout>

You can use one more layout element to merge ImageView and View elements. 您可以再使用一个布局元素来合并ImageViewView元素。 Like this; 像这样;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/titletab"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="@dimen/slidingtab_icon_header"
    android:gravity="center">

    <RelativeLayout
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="@dimen/slidingtab_item_icon_size"
            android:layout_height="@dimen/slidingtab_item_icon_size"
            android:src="@drawable/ic_event_black_18dp"/>

        <View
            android:layout_below="@+id/icon"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/sliding_tab_border_color"/>

    </RelativeLayout>

</RelativeLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM