【问题标题】:How can I display two images side by side in Android?如何在 Android 中并排显示两个图像?
【发布时间】:2012-10-01 10:24:23
【问题描述】:

我正在尝试让我的 xml 让我的 android 应用程序正常工作,但我遇到了一些问题。我正在尝试以线性布局并排显示相同大小的图像。它们之间必须有一个边距,左右两边,顶部。

如何制作这样的 xml 文件,使其通用且适用于任何屏幕尺寸?

更新:这是我的 xml 文件的摘录:

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

<ImageView
        android:id="@+id/area"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:contentDescription="@string/app_name"
        android:src="@drawable/area_selector"
        android:layout_gravity="left" 
        android:paddingTop="15dp"/>

    <ImageView
        android:id="@+id/volume"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:contentDescription="@string/app_name"
        android:src="@drawable/volume_selector"
        android:layout_gravity="left" 
        android:paddingTop="15dp" />

    </LinearLayout>

【问题讨论】:

  • 如果包含layout xml文件会更容易帮助到你。

标签: android xml imageview


【解决方案1】:

您需要为两个图像提供相同的权重,如下所示

  <?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="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="20dp"
        android:layout_weight="1"
        android:src="@drawable/input_cell" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:layout_weight="1"
        android:src="@drawable/input_cell" />

</LinearLayout>

在你的情况下使用这个

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

    <ImageView
            android:id="@+id/area"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:contentDescription="@string/app_name"
            android:src="@drawable/area_selector"
            android:layout_weight="1"
            android:layout_gravity="left" 
            android:paddingTop="15dp"/>

        <ImageView
            android:id="@+id/volume"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="wrap_content" 
            android:contentDescription="@string/app_name"
            android:src="@drawable/volume_selector"
            android:layout_gravity="left" 
            android:paddingTop="15dp" />

        </LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 2018-12-21
    相关资源
    最近更新 更多