简体   繁体   English

将图片居中对齐,右下和左下

[英]Align pictures top center, bottom right and bottom left

I'm trying to align 3 pictures in a LinearLayout so that one is centered at the top, another is at the bottom aligned to the left side and the last is at the bottom aligned to the right side. 我正在尝试在LinearLayout中对齐3张图片,以使其中一张位于顶部居中,另一张位于底部与左侧对齐,而最后一张位于底部与右侧对齐。 Pretty much like a pyramid. 就像金字塔一样。 How could I do this? 我该怎么办? Can you provide an example? 你能举个例子吗?

ie

 X
X X

Use this one: 使用这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/ic_launcher" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right" >

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>
  </LinearLayout>

 </LinearLayout>

You need a RelativeLayout , not a LinearLayout. 您需要一个RelativeLayout ,而不是LinearLayout。

Image A must specify the attribute 图片A必须指定属性

android:layout_alignParentTop="true".  

Image B must specify the attributes 图片B必须指定属性

android:layout_alignParentBottom="true"

and

android:layout_alignParentLeft="true".  

Image C must specify the attributes 图片C必须指定属性

android:layout_alignParentBottom="true"

and

android:layout_alignParentRight="true"  

It will be more flexible if you do it in RelativeLayout but if you want to do it in LinearLayout here is the solution: 如果您在RelativeLayout中执行此操作将更加灵活,但如果要在LinearLayout中执行此操作,则可以使用以下解决方案:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center_horizontal"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM