简体   繁体   English

XML Android中可滚动的完全对称的ImageButton

[英]Scrollable Perfectly Symmetrical ImageButtons in XML Android

In my Android app, I want to create a scrollable grid of images that have the same width/height. 在我的Android应用中,我想创建一个具有相同宽度/高度的可滚动图像网格。 I don't want to use GridView because there will not be that many images. 我不想使用GridView,因为不会有太多图像。 So far, I have: 到目前为止,我有:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="2">
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:scaleType="fitCenter"
                android:background="@null"
                android:src="@drawable/food"/>

            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:scaleType="fitCenter"
                android:background="@null"
                android:src="@drawable/music" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:weightSum="2">
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:scaleType="fitCenter"
                android:background="@null"
                android:src="@drawable/offcampus"/>

            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:scaleType="fitCenter"
                android:background="@null"
                android:src="@drawable/sports" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

It is very close to what I want to achieve, but the second LinearLayout (the one with the layout_height of 0dp) causes the first two ImageButtons to take up the majority of the screen, so that the user has to scroll a lot to see the next two ImageButtons. 它非常接近我想要实现的目标,但是第二个LinearLayout(layout_height为0dp的那个)使前两个ImageButton占据了屏幕的大部分,因此用户必须滚动很多才能看到接下来的两个ImageButton。 In other words, there is way too much space between "rows" of images. 换句话说,图像“行”之间的空间太大。 Is there any way I can fix this? 有什么办法可以解决这个问题?

I attached a picture in case there was confusion. 我附上一张照片,以防万一。

在此处输入图片说明在此处输入图片说明

Thanks for your help. 谢谢你的帮助。

Check if the extra space is due to the LinearLayout being too tall or if the ImageButton s themselves are. 检查多余的空间是否是由于LinearLayout太高或ImageButton本身造成的。 You can use "Dump View Hierarchy" button in the ADT in Eclipse's Devices view. 您可以在Eclipse的“ 设备”视图的ADT中使用“转储视图层次结构”按钮。

I suspect it's the ImageButton s using the images' original height. 我怀疑这是使用图像原始高度的ImageButton See if setting android:adjustViewBounds on the ImageButton s help with the issue. 查看是否在ImageButton的帮助下设置android:adjustViewBounds来解决该问题。 See reference: http://developer.android.com/reference/android/widget/ImageView.html#attr_android:adjustViewBounds 请参阅参考资料: http : //developer.android.com/reference/android/widget/ImageView.html#attr_android : adjustViewBounds

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

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