将按钮高度设为与按钮宽度相等

3

我在互联网上看到了很多这样的问题,但所有的解决方案都对我无效...

所以问题来了。我想要一个按钮,它的宽度是屏幕的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>

我已经尝试过这些方法,但没有成功。

那么我做错了什么?


int btnSize = button1.getLayoutParams().width; 与 int btnSize = button1.getWidth; 得到的结果相同吗? - matty357
我不知道,可能是这样。但btnSize没有获取任何值。无论是使用getLayoutParams().width还是getWidth。 - Hedva
尝试以编程方式执行,就像我的答案一样。我已经用它来调整许多东西的大小。 - matty357
4个回答

5

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

你可以创建这个ResizableButton类:

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.stackoverflow.ResizableButton
        android:id="@+id/button1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:text="Test" />

</LinearLayout>

如果您想要一个高度与宽度相同的按钮,并且想要对该按钮进行操作,则需要在主活动中声明一个ResizableButton对象而不是Button对象。这意味着:

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

而现在,就是这样了。


顺便提一下,<com.example.stackoverflow.ResizableButton 只是你应该创建的类的完整名称,我只是举了一个示例包名。 - Mohamed Hamdaoui
终于有东西对我有用了。非常感谢你! - Hedva

1
你能不能通过编程来设置宽度和高度?而不是在布局中设置?
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);

这将是您的上下文。


0
这里是Mohamed Hamdaoui回答的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)
    }

}

0
一个不太为人知的简单解决方案是使用PercentRelativeLayout来获得正方形布局,你可以完全在XML中实现:
<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'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接