设置按钮高度等于按钮宽度

Set button height equal to the button width

我在网上看到过很多这个问题,但所有的解决方案都不适合我..

这就是问题所在。我想要一个占屏幕宽度 50% 的按钮(效果很好)。

但现在我希望它的高度与宽度相同。

不知何故这不起作用:

Button button1 = (Button) findViewById(R.id.button1);
int btnSize = button1.getLayoutParams().width;
button1.setLayoutParams(new LinearLayout.LayoutParams(btnSize, btnSize));

查看代码:

Activity1.java:

package nl.jesse.project1;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.LinearLayout;


public class Activity1 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity1);

        Button button1 = (Button) findViewById(R.id.button1);
        int btnSize = button1.getLayoutParams().width;
        System.out.println(btnSize);
        //button1.setLayoutParams(new LinearLayout.LayoutParams(btnSize, btnSize));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_activity1, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

Activity_activity1.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activity1"
    android:weightSum="1.0" >


    <Button
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:layout_width="0dip"
        android:text="@string/button1"
        android:id="@+id/button1" />

</LinearLayout>

我已经尝试过这个和其中的一部分,但没有成功。

那我做错了什么?

你能不能只以编程方式设置宽度和高度?而不是在布局中设置它?

Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics displayMetrics = Main.this.getResources().getDisplayMetrics();
Point size = new Point();
display.getSize(size);
int width = size.x;

button1.setWidth(width/2);
button1.setHeight(width/2);

Main.this 将是您的上下文。

好的,我知道这可能不是最有效的方法,但它保证有效。

你创建这个 ResizableButton class :

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Button;

public class ResizableButton extends Button {

    public ResizableButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(width, width);
    }
}

在你的 Activity_activity1.xml 上,你输入:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1.0"
    tools:context=".Activity1">

    <com.example.Whosebug.ResizableButton
        android:id="@+id/button1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:text="Test" />

</LinearLayout>

然后你有一个始终具有与宽度相同的高度的按钮,如果你想使用该按钮,在你的主 activity 上你将必须贴花一个 ResizableButton 对象而不是一个按钮。含义:

 ResizableButton button1 = (ResizableButton) findViewById(R.id.button1);

瞧瞧。

获得方形布局的一个不太为人所知的简单解决方案是使用 PercentRelativeLayout,您可以在 XML:

中 100% 做到这一点
<android.support.percent.PercentRelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        app:layout_aspectRatio="100%"
        app:layout_widthPercent="100%" />
</android.support.percent.PercentRelativeLayout>

确保将库包含在您的应用 gradle 文件中:

compile 'com.android.support:percent:XX.X.X'

这是 答案的 kotlin 版本。

import android.content.Context
import android.util.AttributeSet
import android.widget.Button

public class ResizableButton(context: Context, attrs: AttributeSet) : Button(context, attrs) {

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val width = MeasureSpec.getSize(widthMeasureSpec)
        setMeasuredDimension(width, width)
    }

}