开发者

ScaleToFit Layout Quandry

开发者 https://www.devze.com 2023-04-06 13:42 出处:网络
I\'m try learn to more about the Android layout system; this is a learning experience for me. I\'m currently trying to create a layout which basically amounts to two linear layouts: A fill_parent vert

I'm try learn to more about the Android layout system; this is a learning experience for me. I'm currently trying to create a layout which basically amounts to two linear layouts: A fill_parent vertical, with an inner fill_width horizontal, so that I have an ImageView banner [in the outer], then seven ImageButton columns filling the width of the view [in the inner].

My issue comes, as I need the ImageButton's content to proportionally fill the entire button view [the columns], but respect the ImageButton boundaries, so that if either a small or big image is t开发者_StackOverflow中文版he source, it will be centered in the ImageButton, filling both it's vertical and horizontal dimensions, and be centered. -I have no control over the source images.

I thought that CenterCrop would do it, but nada for me...

ImageView.ScaleType CENTER_CROP Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding).

Any related thoughts on what I have; ignore the outer outer layout please:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:id="@+id/linearLayout2"
        android:layout_height="fill_parent" android:orientation="vertical"
        android:layout_width="fill_parent" android:background="@color/special_grey"
        android:layout_margin="0dp">

        <ImageView android:id="@+id/imageView1"
            android:layout_height="wrap_content" android:layout_gravity="center"
            android:layout_width="wrap_content" android:layout_margin="10dp"
            android:src="@drawable/banner_logo" />

        <LinearLayout android:id="@+id/linearLayout1"
            android:layout_margin="5dp" android:layout_gravity="center"
            android:layout_width="fill_parent" android:layout_height="250dp" android:baselineAligned="true">
            <ImageButton android:minWidth="20dp"
                android:adjustViewBounds="true" android:layout_margin="0dp"
                android:src="@drawable/sample_0" android:maxWidth="100dp"
                android:id="@+id/imageButton1" android:layout_weight="1"
                android:scaleType="centerCrop" android:cropToPadding="true"
                android:layout_gravity="center" android:layout_width="0dp"
                android:padding="0dp" android:layout_height="250dp" />
            <ImageButton android:minWidth="20dp"
                android:adjustViewBounds="true" android:layout_margin="0dp"
                android:src="@drawable/sample_1" android:maxWidth="100dp"
                android:id="@+id/imageButton2" android:layout_weight="1"
                android:cropToPadding="true"
                android:layout_width="0dp"
                android:padding="0dp" android:layout_height="250dp" android:scaleType="centerCrop" android:scrollbarAlwaysDrawHorizontalTrack="false" android:scrollbarAlwaysDrawVerticalTrack="false" android:layout_gravity="fill"/>
            <ImageButton android:minWidth="20dp"
                android:adjustViewBounds="true" android:layout_margin="0dp"
                android:src="@drawable/sample_2" android:maxWidth="100dp"
                android:id="@+id/imageButton3" android:layout_weight="1"
                android:cropToPadding="true" android:layout_gravity="center"
                android:layout_width="0dp" android:padding="0dp"
                android:layout_height="250dp" />
            <ImageButton android:minWidth="20dp"
                android:adjustViewBounds="true" android:layout_margin="0dp"
                android:src="@drawable/sample_3" android:maxWidth="100dp"
                android:id="@+id/imageButton4" android:layout_weight="1"
                android:cropToPadding="true" android:layout_gravity="center"
                android:layout_width="0dp" android:padding="0dp"
                android:layout_height="250dp" />
            <ImageButton android:minWidth="20dp"
                android:adjustViewBounds="true" android:layout_margin="0dp"
                android:src="@drawable/sample_4" android:maxWidth="100dp"
                android:id="@+id/imageButton5" android:layout_weight="1"
                android:cropToPadding="true" android:layout_gravity="center"
                android:layout_width="0dp" android:padding="0dp"
                android:layout_height="250dp" />
            <ImageButton android:minWidth="20dp"
                android:adjustViewBounds="true" android:layout_margin="0dp"
                android:src="@drawable/sample_5" android:maxWidth="100dp"
                android:id="@+id/imageButton6" android:layout_weight="1"
                android:cropToPadding="true" android:layout_gravity="center"
                android:layout_width="0dp" android:padding="0dp"
                android:layout_height="250dp" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>


My comments are only small suggestions as I'm not really sure what you want to achieve with this layout or how it should look like eventually, maybe you could elaborate a little bit or add a small image for a better understanding.

I think your linearLayout1 is missing an orientation, but this shouldn't be critical as it should default to vertical (I think).

Couldn't you put the Image of your ImageView as a backgroundImage for your linearLayout2? Then you could skip the ImageView.

Then I would put most of the ImageButton attributes into a styles-definition. This allows you to change settings much easier and cleans up your code ([Android Developers: Applying Styles and Themes])1

As I have said before, maybe you could elaborate a little bit more about how it should look like, then it will be easier to help.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号