开发者

Android: Fit in three large imageviews

开发者 https://www.devze.com 2023-02-20 09:23 出处:网络
I have an imageview that displays an image that covers about half the screen. This is the class: public class DialButton2 extends ImageView{

I have an imageview that displays an image that covers about half the screen. This is the class:

public class DialButton2 extends ImageView{

    public DialButton2(Context context) {
        super(context);
        this.setImageResource(R.drawable.dialpad);
    }

    public DialButton2(Context context, AttributeSet attrs){
        super(context, attrs);
        this.setImageResour开发者_Go百科ce(R.drawable.dialpad);
    }

}

My problem is how to fit THREE of these in on the screen. If I add one of these in using xml, I can use wrap_content and it will wrap content, I can use fill_parent and it will scale up and fill parent, but if I need to fit in three in a row, how should I force xml to scale them and fit them all in as a group of three? My current code doesnt work, it puts two in a row in and leaves out the third (because it occupies space outside of the screen):

<?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"
    >
<com.com.com.DialButton2
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitCenter"
    />
<com.com.com.DialButton2
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitCenter"
    />
<com.com.com.DialButton2
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitCenter"
    />
</LinearLayout>


<com.com.com.DialButton2
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
android:layout_weight="1"
/>

Use this for all 3 views


You can use layout weights (equal ones for all 3 subviews) while setting their layout widths to 0px. This will make them each take up proportional measures to combine to fill the available space.

0

精彩评论

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