简体   繁体   English

Android布局与方形按钮

[英]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 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来实现这一点,但按钮最终使用正确的宽度,但仍然具有背景的高度(不再是正方形)。

<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 它看起来像这样: 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. 我的解决方案类似于第一个,但是,我在onLayout方法中进行了大小调整。 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 为方便起见,我将我的解决方案包装在一个不错的Android库中,请参阅https://github.com/ronaldsteen/Android-SquareLayout-Library

I recommend the following way, it's straightforward and works 我推荐以下方式,它很简单并且有效

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);
   }

}

I'm usually doing things like that: 我经常这样做:

View v; // the view i want to layout squared
v.measure(1,1);
int size = v.getMeasuredHeight(); // or v.getMeasuredWidth()...
LayoutParams params = ... // either your own, or returned from View
params.width = size;
params.height = size;
v.setLayoutParams(params);

This is certainly not the most sophisticated code, as it leaves most capabilities that can be exploited in MeasureSpec, but it gets the job done, as 'v.measure(1,1)' just says "Measure my view as it is!". 这当然不是最复杂的代码,因为它留下了可以在MeasureSpec中利用的大多数功能,但是它完成了工作,因为'v.measure(1,1)'只是说“测量我的视图!” 。

The absolute plus in my approach is that you don't have to subclass anything and you don't have to add some OnGlobalLayoutListener to your ViewTreeObserver . 我的方法的绝对OnGlobalLayoutListener是您ViewTreeObserverOnGlobalLayoutListener子类,并且您不必向OnGlobalLayoutListener添加一些ViewTreeObserver This works all at the spot where you build or inflate your layout in code. 这适用于您在代码中构建或扩充布局的所有位置。

You can achieve this using ConstraintLayout . 您可以使用ConstraintLayout实现此目的。 You should just add vertical guideline with the position of 50% screen width, align your buttons to the left and right side of the guideline and size them in 1:1 aspect ratio. 您应该添加50%屏幕宽度的垂直指南,将按钮对准指南的左侧和右侧,并以1:1的宽高比调整大小。 Everything can be done in layout XML file, no additional source code required. 一切都可以在布局XML文件中完成,不需要额外的源代码。

Once you've completed all of these steps, you'll end up with something like this: 完成所有这些步骤后,您最终会得到以下结果:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_orange_light"
        android:text="1"
        app:layout_constraintDimensionRatio="H,1:1"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_light"
        android:text="2"
        app:layout_constraintDimensionRatio="W,1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_green_dark"
        android:text="3"
        app:layout_constraintDimensionRatio="W,1:1"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button1" />

    <Button
        android:id="@+id/button4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_purple"
        android:text="4"
        app:layout_constraintDimensionRatio="W,1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/guideline"
        app:layout_constraintTop_toBottomOf="@+id/button2" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />

</android.support.constraint.ConstraintLayout>

Here's how this layout looks on smallest vs largest screens: 以下是此布局在最小屏幕和最大屏幕上的显示方式:

结果视图

ok, here's a revised example: 好的,这是一个修改过的例子:

<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent" android:layout_width="fill_parent">
    <RelativeLayout android:id="@+id/magaLoginLayout"
        android:layout_height="fill_parent" android:layout_width="fill_parent">
        <Button android:text="@+id/Button01" android:id="@+id/Button01"
            android:layout_width="160dip" android:layout_height="160dip" android:layout_marginTop="20dip"></Button>
        <Button android:text="@+id/Button03" android:layout_below="@+id/Button01" android:id="@+id/Button03"
            android:layout_alignLeft="@+id/Button01" android:layout_height="160dip" android:layout_width="160dip"></Button>
        <Button android:text="@+id/Button04" android:layout_below="@+id/Button01" android:id="@+id/Button04"
            android:layout_toRightOf="@+id/Button03" android:layout_height="160dip" android:layout_width="160dip"></Button>
        <Button android:text="@+id/Button02" android:id="@+id/Button02" android:layout_width="wrap_content"
            android:layout_toRightOf="@+id/Button01" android:layout_alignTop="@+id/Button01" android:layout_alignParentRight="true" android:layout_height="160dip"></Button>


</RelativeLayout>

</ViewFlipper>

替代文字

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

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