[英]ImageView does not align properly
I have the following design where I want my left user image and then textviews on the right of the user image and arrow image on the right-most. 我有以下设计,我想要我的左用户图像,然后在用户图像的右边有textviews,在最右边的是箭头图像。 However, for some reasons, arrow does not come to the right-most
但是,由于某些原因,箭头没有出现在最右边
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dip"
android:background="#FFFFFF">
<ImageView
android:id="@+id/userimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip"
android:gravity="center"
android:layout_alignParentLeft="true"
android:src="@drawable/ic_person_black_24dp"
android:layout_gravity="center_vertical" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dip">
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="15dp"
android:textColor="#040404"
android:typeface="sans"
android:textStyle="bold"
android:text="Username_05"
android:singleLine="true" />
<TextView
android:id="@+id/secondline"
android:text="Text"
android:layout_below="@id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:textColor="#000000"
android:singleLine="true" />
</LinearLayout>
<ImageView
android:id="@+id/icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/ic_keyboard_arrow_right_black"
android:layout_gravity="center_vertical" />
</LinearLayout>
Here is my current output: 这是我当前的输出:
Update 1: 更新1:
Keep it simple. 把事情简单化。 Use LinearLayout with horizontally orientation instead of RelativeLayout and add 1. ImageView 2. Another LinearLayout with layout_weight=1 and vertical orientation which will contain the two TextViews 3. ImageView
使用LinearLayout而不是RelativeLayout的水平方向,并添加1。ImageView2。另一个layout_weight = 1且垂直方向的LinearLayout将包含两个TextViews3。ImageView
Sorry for not posting any code. 抱歉,没有发布任何代码。 I'm from mobile ;)
我来自手机;)
I think it is because you use android:layout_width="match_parent" instead of wrap content. 我认为这是因为您使用android:layout_width =“ match_parent”而不是包装内容。
If you use linear Layout, you need a weight to use all the space: 如果使用线性布局,则需要权重才能使用所有空间:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="6dip"
android:background="#FFFFFF">
<ImageView
android:id="@+id/userimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip"
android:gravity="center"
android:layout_alignParentLeft="true"
android:src="@drawable/ic_person_black_24dp"
android:layout_gravity="center_vertical" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="6dip">
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="15dp"
android:textColor="#040404"
android:typeface="sans"
android:textStyle="bold"
android:text="Username_05"
android:singleLine="true" />
<TextView
android:id="@+id/secondline"
android:text="Text"
android:layout_below="@id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:textColor="#000000"
android:singleLine="true" />
</LinearLayout>
<ImageView
android:id="@+id/icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/ic_keyboard_arrow_right_black"
android:layout_gravity="center_vertical" />
</LinearLayout>
I think you are looking for something like this 我想你正在寻找这样的东西
. 。 There is no point creating a separate Imageview to accomodate the icons.
没有必要创建单独的Imageview来容纳图标。 Better use the
android:drawableLeft
and android:drawableRight
to achieve that. 最好使用
android:drawableLeft
和android:drawableRight
来实现。
Below is the sample code: 下面是示例代码:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|center"
android:text="Testing"
android:drawableLeft="@drawable/ic_account_circle_black_24dp"
android:drawableRight="@drawable/ic_account_circle_black_24dp"/>
Edit: For multiline : 编辑:对于多行:
First, do not use android:orientation
on a RelativeLayout
, that property is only for LinearLayout
. 首先,请勿在
RelativeLayout
上使用android:orientation
,该属性仅适用于LinearLayout
。
Second, you are doing circular references: 其次,您正在做循环引用:
android:layout_above="@id/secondline"
android:layout_above="@id/secondline"
android:layout_below="@id/username"
android:layout_below="@id/username"
When you do that the system does not know how to render the views, because they depend on each other. 这样做时,系统不知道如何渲染视图,因为它们彼此依赖。
Try doing this: 尝试这样做:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="6dip"
android:background="#FFFFFF">
<ImageView
android:id="@+id/userimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip"
android:layout_alignParentLeft="true"/>
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textStyle="bold"
android:text="Username_05"
android:layout_toRightOf="@id/userimage"
android:singleLine="true" />
<TextView
android:id="@+id/secondline"
android:text="Text"
android:layout_below="@id/username"
android:layout_toRightOf="@+id/userimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"/>
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Result: 结果:
Just use layout_weight. 只需使用layout_weight。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="horizontal"
android:padding="6dip">
<ImageView
android:id="@+id/userimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_gravity="center_vertical"
android:layout_marginRight="6dip"
android:layout_weight=".1"
android:gravity="center"
android:src="@android:drawable/ic_menu_my_calendar" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:orientation="vertical"
android:padding="6dip">
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:singleLine="true"
android:text="Username_05"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/secondline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/username"
android:singleLine="true"
android:text="Text"
android:textColor="#000000"
android:textSize="10sp" />
</LinearLayout>
<ImageView
android:id="@+id/icon"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:layout_weight="1"
android:src="@android:drawable/arrow_down_float" />
</LinearLayout>
Use the code you provided in your question but change android:layout_width="match_parent"
to android:layout_width="wrap_content"
in the ImageView
of your arrow. 使用您在问题中提供的代码,但在箭头的
ImageView
中将android:layout_width="match_parent"
更改为android:layout_width="wrap_content"
。 This should make the arrow ImageView
appear at the complete right 这应该使箭头
ImageView
出现在完全右侧
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.