Android 如何使用两个文本视图将ImageView居中?
xml是如何做到这一点的Android 如何使用两个文本视图将ImageView居中?,android,android-layout,Android,Android Layout,xml是如何做到这一点的 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="5" > <LinearLayout android:layout_width="fill_parent" android:layout_height="match_pare
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="4"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
也就是说,我有两个文本视图,一个在另一个下面,都向左对齐。另外,在同一行,我有一个右边对齐的ImageView。最后,我需要垂直居中的文本视图的图像 以RelativeLayout为父对象,在父对象左侧的文本视图中,在另一个视图的下方各取两个。并将ImageView放置在父级右侧,垂直居中
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="4"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
选中此项:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview1"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:textColor="#000000"
android:textSize="17sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview2"
android:layout_below="@+id/textview1"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:textColor="#000000"
android:textSize="17sp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="4"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
以RelativeLayout为父对象,在父对象左侧的文本视图中,在另一个文本视图的下方放置两个文本视图。并将ImageView放置在父级右侧,垂直居中
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="4"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
选中此项:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview1"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:textColor="#000000"
android:textSize="17sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview2"
android:layout_below="@+id/textview1"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:textColor="#000000"
android:textSize="17sp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="4"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
试试这个,它非常简单,布局不会在任何分辨率下扭曲,而使用相对布局会扭曲。。。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="4"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
试试这个,它非常简单,布局不会在任何分辨率下扭曲,而使用相对布局会扭曲。。。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="4"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
您可以使用单个TextView小部件实现所需的显示。您可以使用SpannableString或SpannableStringBuilder显示格式与TextView.setCompoundDrawables或TextView.setCompoundDrawables混合的文本,在文本右侧显示图像。您可以通过单个TextView小部件实现所需的显示。您可以使用SpannableString或SpannableStringBuilder显示格式与TextView.setCompoundDrawables或TextView.setCompoundDrawables混合的文本,在文本右侧显示图像。使用@minipif是正确的。你应该使用相对论。从android工程师Romain Guy身上学习——使用@minipif是正确的。你应该使用相对论。从android工程师Romain Guy身上学习-如果可能的话,最好避免多个布局,这样系统需要更多的资源来绘制和重新绘制它们!相关文章:如果可能的话,最好避免多重布局,因为系统需要更多的资源来绘制和重新绘制它们!相关文章:虽然我很感激你在这里的努力,但请不要吝啬。我宁愿给这个人一个方向,让他/她去做。@Abhijit新手总是新手,他们可以从一个或多个实际的事情中学习,而不是Q/a站点。有时,方向可能会导致错误的目的地,我们必须说明编码是如何进行的,更好地解释它。和我做的一样虽然我很感激你在这里的努力,但请不要吝啬。我宁愿给这个人一个方向,让他/她去做。@Abhijit新手总是新手,他们可以从一个或多个实际的事情中学习,而不是Q/a站点。有时,方向可能会导致错误的目的地,我们必须说明编码是如何进行的,更好地解释它。和我做的一样
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="4"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>