2011-06-02 30 views
4

我想为两个imageView并排排列的Android应用程序创建一个Activity。我目前的布局配置如下:Android - 两个ImageViews并排

<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> 

第一个图像将是一个正方形(可以说100×100)和第二图像将是矩形(300x100的) - 我希望他们能够彼此相邻排列,但总是被缩放以适应设备的宽度 - 这可能只是布局配置?

当前的配置只显示第一个图像的整个宽度(和几乎高度)的屏幕和第二个图像根本没有显示。我试图用fill_parent和hardocding宽度来改变wrap_content,但是这只是导致两个图像都显示出来,但是第一个图像位于第二个图像的顶部(都停住了左侧)。

感谢


再次更新:

我已经更新了我的布局看现在这个样子,包括了滚动的建议,但没有喜悦:

<?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显示的屏幕截图:

enter image description here

回答

12

尝试使用layout_weight同时为ImageView组件。所以像这样:

<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

+0

谢谢 - 正如在更新的OP中指出的那样,重量已经正确地缩放了图像,但是我仍然遇到第一个(和更小)图像出现在第二个顶部而不是并排 - 任何想法? – rhinds 2011-06-03 13:49:54

+0

尝试在'ScrollView'中包含'LinearLayout' – binnyb 2011-06-03 15:06:42

+0

仍然没有滚动视图的喜悦 - 我更新了OP以显示当前配置以及UI当前的外观样式的屏幕截图 – rhinds 2011-06-03 20:04:17

0

您可以尝试创建水平ScrollView,其中layout_width的像素值大于两个ImageViews的总和,而不是fill_parent。然后把你的LinearLayout放在里面。