gpt4 book ai didi

android - 在相对布局中居中 ImageView

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:59:35 26 4
gpt4 key购买 nike

我在相对布局中有一个图像和一个 TextView 。图像和 TextView 占屏幕宽度的 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

最佳答案

试试这段代码,它会满足您的要求

<?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>

关于android - 在相对布局中居中 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41652854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com