Android layout with square buttons
I want to make a layout similar to this one:
www.ImageBanana.net - layout.png http://www.imagebanana.com/img/9kmlhy66/thumb/layout.png
Four square buttons on the screen - each of those using half of the screen with/screen height (whichever is smaler). Independent of screen size/resolution.
I already tried to achieve this by using a LinearLayout
but the buttons are ending up using the correct width, but still having the height of the background (not square any more).
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_height="wrap_content"
style="@style/CKMainButton"
android:layout_width="fill_parent"
android:text="@string/sights"
android:id="@+id/ApplicationMainSight"
android:layout_toLeftOf="@+id/ApplicationMainEvent"></Button>
<Button
android:layout_height="wrap_content"
style="@style/CKMainButton"
android:layout_width="fill_parent"
android:text="@string/sights"
android:id="@+id/ApplicationMainSight"
android:layout_toLeftOf="@+id/ApplicationMainEvent"></Button>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_height="wrap_content"
style="@style/CKMainButton"
android:layout_weight="1"
android:layout_width="fill_parent"
android:text="@string/usergenerated"
android:id="@+id/ApplicationMainUserGenerated" />
<Button
android:layout_height="wrap_content"
style="@style/CKMainButton"
android:layout_weight="1"
android:layout_width="fill_parent"
android:text="@string/tours"
android:id="@+id/ApplicationMainTour"/>
</LinearLayout>
</LinearLayout>
It's looking like this: www.ImageBanana.net - layout2.png http://www.imagebanana.com/img/i2ni6g4/thumb/layout2.png
How can i acchieve the Layout to look like the image at the top above?
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
public class SquareLayout extends LinearLayout {
// Desired width-to-height ratio - 1.0 for square
private final double mScale = 1.0;
public SquareLayout(Context context) {
super(context);
}
public SquareLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (width > (int)((mScale * height) + 0.5)) {
width = (int)((mScale * height) + 0.5);
} else {
height = (int)((width / mScale) + 0.5);
}
super.onMeasure(
MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
);
}
}
My solution is similar to the first one, however, I do the resizing in the onLayout method. Don't know which one is better.
For convenience I've wrapped my solution in a nice Android Library, see https://github.com/ronaldsteen/Android-SquareLayout-Library
我推荐以下方式,它很简单,而且很有效
public class SquareLayout extends RelativeLayout {
public SquareLayout(Context context) {
super(context);
}
public SquareLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
}
链接地址: http://www.djcxy.com/p/84704.html
上一篇: Android的ImageView不尊重maxWidth?
下一篇: Android的布局与方形按钮