简体   繁体   English

如何将4 imageview与textview水平对齐

[英]how to horizontally align 4 imageview with textview

I want to show four ImageView s horizontally together with TextView s. 我想与TextView一起水平显示四个ImageView This is my screen and sample screen which I want to make. 这是要制作的屏幕和示例屏幕。 How can I do that? 我怎样才能做到这一点?

在此处输入图片说明

XML code: XML代码:

 <LinearLayout
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="#CCCCCC"
  android:layout_gravity="center"
  >

<ImageView
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:src="@drawable/ic_launcher"
    />
<ImageView
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:src="@drawable/ic_launcher"
    />
 <ImageView
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher"
    />
    <ImageView
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:src="@drawable/ic_launcher" 
    />
 </LinearLayout>

You could wrap each ImageView/TextView pair in a LinearLayout like this: 您可以将每个ImageView / TextView对包装在LinearLayout中,如下所示:

<LinearLayout
  android:id="@+id/group1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:gravity="center_horizontal>
  <ImageView
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher"
    />
  <TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@strings/my_text" />
</LinearLayout>

Edit: Added gravity attribute to keep everything in line 编辑:添加重力属性以使所有内容保持一致

This works. 这可行。 I checked. 我检查了。

  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="#CCCCCC"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hai there" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hai there" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hai there " />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hai there" />
    </LinearLayout>
</LinearLayout>

Use TableRow and place all the images in that. 使用TableRow并将所有图像放置在其中。 Don't forget to give android:layout weight=1. 别忘了给android:layout weight = 1。

<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="match_parent" >

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

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

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

<ImageView
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />
</TableRow>

Use this 用这个

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


<ImageView
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:src="@drawable/ic_launcher"
    android:layout_weight=".25f"
    />


<ImageView
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:src="@drawable/ic_launcher"
     android:layout_weight=".25f"
    />
 <ImageView
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher"
      android:layout_weight=".25f"
    />
    <ImageView
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:src="@drawable/ic_launcher" 
     android:layout_weight=".25f"
    />
 </LinearLayout>

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

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