【问题标题】:Set button height and width as wrap content and fill parent将按钮高度和宽度设置为包装内容并填充父项
【发布时间】:2012-07-26 13:17:07
【问题描述】:

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

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

【问题讨论】:

    标签: java android height width


    【解决方案1】:

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

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

    【讨论】:

    • 我收到错误“未知实体'布局参数'。这是为什么?
    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
    • @JuJoDi 我已经从链接中添加了代码。感谢您的意见
    【解决方案2】:

    您需要寻找的是 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>
    

    【讨论】:

    • 非常感谢!我非常感谢您花时间编写示例代码,使事情变得非常清楚。你是最棒的!
    猜你喜欢
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 2018-10-24
    • 2011-04-07
    相关资源
    最近更新 更多