【问题标题】:Center ImageView in Relative Layout在相对布局中居中 ImageView
【发布时间】:2017-01-14 17:26:02
【问题描述】:

我在相对布局中有一个图像和一个文本视图。图像和文本视图占据了屏幕宽度的 50%。如何使图像和 textview 居中?尝试了很多方法,不知道自己做错了什么……

这是我的代码:

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="100">
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:layout_centerHorizontal="true">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/textViewEventImage"
                android:adjustViewBounds="true" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:background="@color/eventNameBackground"
                android:id="@+id/textViewEventName"
                android:textColor="@android:color/white"
                android:textSize="15sp"/>
        </RelativeLayout>
    </LinearLayout>

enter image description here

【问题讨论】:

  • 你想让你的图片成为屏幕的中心吗?

标签: android xml imageview


【解决方案1】:

试试这个符合你要求的代码

<?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="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:background="#ff0000" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="180dp"
        android:layout_height="120dp"
        android:layout_centerInParent="true"
        android:gravity="center_vertical"
        android:src="@drawable/americanexpress" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="16dp"
        android:text="Your product" />
</RelativeLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5"
    android:background="#00ff00" />

</LinearLayout>

【讨论】:

    【解决方案2】:

    这样试试

    imageview设为

    centerinparent ="true"
    

    还有textview还有centerinparent="true"

    并为textview添加一个属性 作为

    android: layout_below="@+id/imageview id"
    

    【讨论】:

    • 那不行.. imageview 仍然在屏幕的左侧,而 textview 在 imageview 的中间..
    【解决方案3】:

    也许这会有所帮助

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true">
        <ImageView
            android:layout_width="xxxdp"
            android:layout_height="xxxdp"
            android:src="@drawable/someimage"
            />
        <TextView
            android:layout_width="xxxdp"
            android:layout_height="xxxdp" 
            android:text="fweqf"/>
        </LinearLayout>
    
    </RelativeLayout>
    

    【讨论】:

    • 我必须使用相对布局,因为我想在图像视图上显示文本...
    • 我的答案将产生结果作为您引用的图像,该图像使用的是包含一个线性布局视图的相对布局。无论如何,“图像视图上的文本”是什么意思?你想让你的图像看起来像你的文本背景吗?我很想知道更多关于你想做什么。
    【解决方案4】:

    如果你想让 ImageView 和 TextView 居中,那就用这个吧。

    <?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">
    
        <ImageView
            android:id="@+id/textViewEventImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@mipmap/ic_launcher" />
    
        <TextView
            android:id="@+id/textViewEventName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textViewEventImage"
            android:layout_centerInParent="true"
            android:text="There is Your Text"
            android:textSize="15sp" />
    </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 2015-02-04
      • 1970-01-01
      • 1970-01-01
      • 2012-12-17
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多