繁体   English   中英

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

[英]Set button height and width as wrap content and fill parent

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

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

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

活动:

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

布局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>

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

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

暂无
暂无

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

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