[英]Android - ImageView not align properly in TableLayout
My ImageView
and TextView
not align properly. 我的
ImageView
和TextView
不能正确对齐。 Is it because of my layout setting? 是因为我的布局设置吗? What I want is ImageView align parent left and TextView right of ImageView.
我想要的是ImageView在ImageView的左对齐父级和TextView的右对齐。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:layout_marginTop="10dp"
android:padding="10dp" >
<ImageView
android:id="@+id/profilePicture"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginRight="8dp"
android:src="@drawable/user_50"
android:gravity="center" ></ImageView>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Mohammad Nurdin, 26"
android:gravity="center"/>
</TableRow>
<!-- .. -->
</TableLayout>
</ScrollView>
I tried something like this and it is according to your requirement at least 我尝试过这样的事情,至少根据您的要求
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:layout_marginTop="10dp"
android:padding="10dp" >
<ImageView
android:id="@+id/profilePicture"
android:layout_width="300dp"
android:layout_height="80dp"
android:layout_marginRight="8dp"
android:src="@drawable/user_50"
android:gravity="center"
android:layout_gravity="left"></ImageView>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:text="Mohammad Nurdin, 26"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_column="70"
android:layout_gravity="center" />
</TableRow>
Hope it helps ... :) 希望能帮助到你 ... :)
Add this to your TableLayout : android:stretchColumns="0,1"
and this is optional in TableRow android:gravity="center"
将此添加到您的TableLayout中:
android:stretchColumns="0,1"
,在TableRow中这是可选的android:gravity="center"
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:stretchColumns="0,1">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:background="#ffffff"
android:padding="5dp">
<ImageView
android:id="@+id/profilePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/user" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mohammad Nurdin, 26" />
</TableRow>
</TableLayout>
The output is like this : 输出是这样的:
I already found the answer. 我已经找到答案了。 Use
LinearLayout
inside TableLayout
. 在
TableLayout
使用LinearLayout
。
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:layout_marginTop="10dp"
android:padding="10dp" >
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_weight="1"
android:id="@+id/profilePicture"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/user_50"
android:gravity="left" ></ImageView>
<TextView
android:layout_weight="100"
android:id="@+id/textView1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="Mohammad Nurdin, 26"/>
</LinearLayout>
</TableRow>
<!-- ? -->
</TableLayout>
Gravity doesn't work for ImageViews in TableLayout like it does for TextViews, what you need to do is wrap the ImageView inside a RelativeLayout, then align the ImageView to its new parent.. so like this.. 重力不适用于TableLayout中的ImageView,而不像TextView中那样,您需要做的是将ImageView包裹在RelativeLayout中,然后将ImageView与其新的父对象对齐。
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4">
<ImageView
android:id="@+id/myImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
<TextView
android:id="@+id/myText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="6"
android:gravity="left"
android:text="hello" />
It's because of gravity 是因为重力
android:gravity="center"
You can change it into 您可以将其更改为
android:gravity="left"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.