java 将按钮高度和宽度设置为换行内容并填充父级

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11694945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:05:40  来源:igfitidea点击:

Set button height and width as wrap content and fill parent

javaandroidheightwidth

提问by SweSnow

I'm developing a application where the user is supposed to be able to alter the apperence of a button by pressing other buttons. I use four buttons to set height as wrap content, height as fill parent, width as wrap content and width as fill parent.

我正在开发一个应用程序,用户应该能够通过按下其他按钮来改变按钮的外观。我使用四个按钮将高度设置为环绕内容,高度设置为填充父级,宽度设置为环绕内容,宽度设置为填充父级。

I googled a bit and found a solution using LayoutParams although that code didn't specify if height of width was alterd. I also got errors saying my IDE couldn't recognise "LayoutParams". What is the best way to do this?

我用谷歌搜索了一下,找到了一个使用 LayoutParams 的解决方案,尽管该代码没有指定宽度的高度是否改变。我还收到错误消息,说我的 IDE 无法识别“LayoutParams”。做这个的最好方式是什么?

回答by Pavel Dudka

What you need to look for - is View.ViewGroup.LayoutParams. Every LayoutParams has constant value for MATCH_PARENT and WRAP_CONTENT attributes. I have prepared simple code sample you can play with:

您需要寻找的是 View.ViewGroup.LayoutParams。每个 LayoutParams 都具有 MATCH_PARENT 和 WRAP_CONTENT 属性的常量值。我准备了简单的代码示例,您可以使用:

Activity:

活动:

package com.example.stack2;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{

    Button test;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b = (Button)findViewById(R.id.button1);
        b.setOnClickListener(this);
        b = (Button)findViewById(R.id.button2);
        b.setOnClickListener(this);
        b = (Button)findViewById(R.id.button3);
        b.setOnClickListener(this);
        b = (Button)findViewById(R.id.button4);
        b.setOnClickListener(this);
        test = (Button)findViewById(R.id.test);
    }
    public void onClick(View v)
    {
        LayoutParams lp = test.getLayoutParams();
        if(v.getId() == R.id.button1)
        {
            lp.height = LayoutParams.WRAP_CONTENT;
        }else if(v.getId() == R.id.button2){
            lp.width = LayoutParams.WRAP_CONTENT;
        }else if(v.getId() == R.id.button3){
            lp.height = LayoutParams.MATCH_PARENT;
        }else if(v.getId() == R.id.button4){
            lp.width = LayoutParams.MATCH_PARENT;
        }
        test.setLayoutParams(lp);
    }
}

Layout xml:

布局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:orientation="vertical" >

    <LinearLayout 
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="h_wc" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="w_wc" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="h_fp" />

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="h_fp" />

    </LinearLayout>

    <Button
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test" />

</LinearLayout>

回答by moDev

By using the below code you can easily alter the height and width dynamically.

通过使用以下代码,您可以轻松地动态更改高度和宽度。

btn = new Button(Activity.this);
btn.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,     LayoutParams.WRAP_CONTENT));

btn.setText("\t\t" + lbl + "\t\t\t  ");
        btn.setBackgroundResource(R.drawable.blue_button);
btn.setwidth(100);