![](/img/trans.png)
[英]Android app, how to center align ImageView in a TableLayout cell programmatically
[英]Android - ImageView not align properly in TableLayout
我的ImageView
和TextView
不能正確對齊。 是因為我的布局設置嗎? 我想要的是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>
我嘗試過這樣的事情,至少根據您的要求
<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>
希望能幫助到你 ... :)
將此添加到您的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>
輸出是這樣的:
我已經找到答案了。 在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>
重力不適用於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" />
是因為重力
android:gravity="center"
您可以將其更改為
android:gravity="left"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.