Android RelativeLayout alignTop不起作用 [英] Android RelativeLayout alignTop doesn't work
问题描述
我试图在RelativeLayout中使用alignTop属性,以便将文字设置在个人资料图片的右侧-与图片的顶部对齐.
I'm trying to use alignTop attribute in a RelativeLayout in order to set a text to right of a profile picture - aligned to picture's top.
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<!-- Home fragment layout -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.coapps.pico"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_dark_green"
android:orientation="vertical" >
<!-- title layout -->
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_fragment_title" >
<!-- profile picture -->
<ImageView
android:id="@+id/fragment_home_profile_picture"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:background="@drawable/background_profile_picture"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
android:src="@drawable/test_pic" />
<!-- user name -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/fragment_home_profile_picture"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/fragment_home_profile_picture"
android:gravity="top"
android:text="Roi Mozer"
android:textColor="@android:color/white"
android:textSize="30sp"/>
</RelativeLayout>
</RelativeLayout>
这是预览中的外观(与手机中的外观相同):
This is how it looks in the preview (and the same in the phone):
您可以看到名称中心与图片的顶部对齐-不知道为什么.
As you can see the name center is aligned to picture's top - don't know why..
如何将它们设置在同一行?
更新: 当我将布局高度更改为给定高度(而不是包装内容)时,它确实起作用...
UPDATE: When i changed the layout height to a given height (and not wrap content) it does work...
推荐答案
问题可能出在您的图像或背景中.我测试了另一个图像,并且两个图像都沿着顶部对齐.
The problem may be in your image or background. I tested with another image and both are aligned along the tops.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.coapps.pico"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- title layout -->
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:background="@drawable/background_fragment_title"
>
<!-- profile picture -->
<ImageView
android:id="@+id/fragment_home_profile_picture"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:src="@drawable/falcao_large"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
/>
<!-- user name -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/fragment_home_profile_picture"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/fragment_home_profile_picture"
android:gravity="top"
android:textColor="@android:color/white"
android:text="Roi Mozer"
android:textSize="30sp"/>
</RelativeLayout>
我认为问题是图像背景.文字与背景对齐
I think problem is image background. The text align to top of background
这篇关于Android RelativeLayout alignTop不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!