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

android– 设置按钮高度等于按钮宽度,第1张

概述我在互联网上看到了很多这个问题,但所有解决方案对我都不起作用..所以这就是问题所在.我想要一个屏幕宽度为50%的按钮(工作正常).但现在我希望它与宽度具有相同的高度.不知怎的,这不起作用:Buttonbutton1=(Button)findViewById(R.id.button1);intbtnSize=button1.getLay

我在互联网上看到了很多这个问题,但所有解决方案对我都不起作用..

所以这就是问题所在.我想要一个屏幕宽度为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>

我已经尝试了这个和部分内容,没有成功.

> How to set same width and height of a button
> ImageView – have height match width?
> Set same height as width at a ToggleButton
> Layout width equal height

那么我做错了什么?

解决方法:

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

您创建此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>

然后你有一个总是与宽度相同的按钮,如果你想玩按钮,你的主要活动你必须decalre Resizablebutton对象而不是按钮.含义:

 Resizablebutton button1 = (Resizablebutton) findVIEwByID(R.ID.button1);

和Voilà.

总结

以上是内存溢出为你收集整理的android – 设置按钮高度等于按钮宽度全部内容,希望文章能够帮你解决android – 设置按钮高度等于按钮宽度所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1110846.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存