【问题标题】:Android - Two ImageViews side-by-sideAndroid - 两个并排的 ImageView
【发布时间】:2011-06-04 03:58:02
【问题描述】:

我正在尝试为一个具有两个并排对齐的 imageViews 的 Android 应用程序创建一个 Activity。我当前的布局配置如下:

<LinearLayout android:orientation="horizontal"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:paddingTop="15dip" android:paddingBottom="15dip"
    android:background="@drawable/dark_bg">

    <ImageView android:id="@+id/numberDays"  
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:scaleType="fitStart"
        android:src="@drawable/counter_01" />
    <ImageView android:src="@drawable/counter_days" 
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:scaleType="fitStart" 
        android:id="@+id/daysText"></ImageView>

</LinearLayout>

第一个图像是正方形(比如说 100x100),第二个图像是矩形(300x100) - 我希望它们彼此相邻对齐,但始终缩放以适应设备的宽度 -仅通过布局配置就可以吗?

当前配置仅显示第一张图像的整个屏幕宽度(几乎是高度),而第二张图像根本不显示。我尝试使用 fill_parent 和 hardocding 宽度更改 wrap_content,但这只是导致两个图像都显示,但第一个图像位于第二个图像的顶部(都锚定在左侧)。

谢谢


再次更新:

我已经更新了我的布局,现在看起来像这样,包括推荐的 ScrollView,但没有乐趣:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:gravity="top"
    android:layout_width="fill_parent" android:layout_height="fill_parent" >

<!-- Header for activity - this has the countdown in it -->
<ScrollView android:id="@+id/ScrollView01" android:layout_height="wrap_content" android:layout_width="fill_parent">
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:gravity="right"
        android:background="@drawable/dark_bg" android:orientation="horizontal">

        <ImageView android:id="@+id/numberDays"  
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_weight="2"
            android:src="@drawable/counter_01" />

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

    </LinearLayout>

</ScrollView>


<!--  main body for the rest of the info -->
    <LinearLayout android:orientation="horizontal" android:gravity="center"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:background="@drawable/light_bg">
    </LinearLayout>

</LinearLayout>

按照建议使用 layout_weight 为两个图像提供了正确的比例并且看起来完美缩放,但是,我仍然遇到问题,即它们都锚定在屏幕的最左侧,所以第一张图像实际上是叠加在第二张图片上,而不是并排放置。

下面是 Eclipse 显示的截图:

【问题讨论】:

    标签: android imageview


    【解决方案1】:

    尝试对两个ImageView 组件使用layout_weight。所以像:

    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:paddingTop="15dip"
        android:paddingBottom="15dip"
        android:background="@drawable/dark_bg">
        <ImageView android:id="@+id/numberDays"  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:scaleType="fitStart"
            android:layout_weight="1"
            android:src="@drawable/counter_01" />
        <ImageView android:src="@drawable/counter_days" 
            android:layout_height="wrap_content" 
            android:layout_width="wrap_content"
            android:scaleType="fitStart" 
            android:layout_weight="1"
            android:id="@+id/daysText"></ImageView>
    </LinearLayout>
    

    我为每个人添加了android:layout_weight="1"Read uplayout_weight 用于 LinearLayout 定义,非常 useful

    【讨论】:

    • 谢谢 - 正如更新的 OP 中所指出的,这个权重已经正确缩放了图像,但是我仍然遇到第一个(和更小的)图像出现在第二个而不是并排的顶部的问题 -有什么想法吗?
    • 尝试将LinearLayout 括在ScrollView
    • 滚动视图仍然没有乐趣 - 我已更新 OP 以显示当前配置和 UI 当前外观的屏幕截图
    • 好的 - 似乎它可能是一个 Eclipse 问题 - 在模拟器上运行它并且图像正确对齐。有趣的是,上面有两张图片的上半部分占据了屏幕的大部分——我原以为它只会和图片的高度一样大。
    • 我已接受此答案,因为图像现在并排显示。但是,仍然存在一个问题 - 虽然两个图像并排缩小,但包含它们的布局(具有深色背景)似乎仍然显示全高而不是换行到缩放图像的大小- 如果我能避免这种情况有什么想法吗?
    【解决方案2】:

    您可以尝试使用像素值大于两个 ImageView 组合的layout_width 创建水平ScrollView,而不是fill_parent。然后将您的LinearLayout 放入其中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 2013-04-23
      相关资源
      最近更新 更多