【问题标题】:Programmatically change the width of the Button in android以编程方式更改android中Button的宽度
【发布时间】:2019-02-01 18:16:16
【问题描述】:

如何以编程方式更改Buttonwidth。我可以更改height,但宽度不会改变。以下是我的代码 sn-p

private void createButton(final String label)
{

    Button button = new Button(this);
    button.setText(label);


    button.setWidth(10);
    button.setHeight(100);

    button.setOnClickListener(new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {


        }
    });
    mMenuContainer.addView(button, mMenuItemLayoutParamters);


}

但是Buttonwidth占据了屏幕的width

【问题讨论】:

    标签: android


    【解决方案1】:

    button.setLayoutParams(new LinearLayout.LayoutParams(10, 100));

    应该对你有用。

    网上有一个很棒的例子:check this


    您应该应用包含您的ViewViewGroup 中的LayoutParams。也就是说,如果你的按钮在RelativeLayout里面,你应该使用RelativeLayout.LayoutParams

    【讨论】:

    • 重要提示:您应该应用包含您的ViewViewGroup 中的LayoutParams。也就是说,如果您的按钮在RelativeLayout 内,则应使用RelativeLayout.LayoutParams
    • 你应该将你的尺寸值乘以显示比例,因为构造函数需要像素,而不是 dp。像这样:widthInPixels = getResources().getDisplayMetrics().density * widthInDp;
    • 必须遵循有关使用正确 ViewGroup 的建议。如果你使用了不正确的 VG,应用会崩溃,并且 logcat 的原因信息为零
    • 我想知道为什么 setWIdth() 和 setHeight() 甚至在按钮和文本视图在实践中似乎不起作用时,包括我自己。我在这个网站上看到几十个“问题:setWIdth() 不起作用,答案:使用 LayoutParams”。
    • 对于 Kotlin 程序员,它应该像这样工作:button.layoutParams = LinearLayout.LayoutParams(10, 100)
    【解决方案2】:

    你可以这样做。

    LinearLayout.LayoutParams params = button.getLayoutParams();
    params.width = 100;
    button.setLayoutParams(params);
    

    但请确保在将按钮添加到父级后执行此操作。当父级是LinearLayout 时使用LinearLayout.LayoutParams,当父级是RelativeLayout 时使用RelativeLayout.LayoutParams

    【讨论】:

      【解决方案3】:
      ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) yourButton.getLayoutParams();
      layoutParams.width = 150;
      yourButton.setLayoutParams(layoutParams);
      

      “ConstraintLayout”可以更改为您的布局并通过“layoutParams.width”设置大小。这个案子对我有用!

      【讨论】:

        【解决方案4】:

        可以这样做

            int width= (int)getResources().getDimension(R.dimen.button_width);
            int heigth= (int)getResources().getDimension(R.dimen.button_heigth);
            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(width, heigth);
            button.setLayoutParams(layoutParams);
        

        【讨论】:

          【解决方案5】:

          我用这个解决了:

          //ImageButton creation
          final ImageButton server_icon_button=new ImageButton(getApplicationContext());
          //setting background image in drawable
          server_icon_button.setBackgroundResource(R.drawable.bonsai);
          //After adding ImageButton to the parent i done this
          server_icon_button.getLayoutParams().height=180;
          server_icon_button.getLayoutParams().width=100;
          

          希望对你有所帮助。

          【讨论】:

            【解决方案6】:

            就我而言,我使用的是 AppCompatImageButton(和 AppCompat)。 事实证明,如果我将密度调整后的图像保存在各种可绘制对象中 文件夹:drawable-ldpi、drawable-mdpi、drawable-hdpi 等。然后按钮不会缩放。 相反,我必须在 drawable-nodpi 中为每个按钮放置一个可绘制对象。 确保drawable-nodpi 按钮图像是您需要的最大 尺寸。 Android 不会放大按钮大小;但是,它可以缩小尺寸。我在 XML 中创建了一个这样的按钮:

            <RelativeLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:meter="http://schemas.android.com/apk/res-auto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            
                <android.support.v7.widget.AppCompatImageButton
                    android:id="@+id/button"
                    style="@style/ButtonStyle"
                    android:baselineAlignBottom="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:minHeight="0dp"
                    android:minWidth="0dp"
                    app:srcCompat="@drawable/button_selector"
                    android:background="?attr/selectableItemBackgroundBorderless"
                    android:contentDescription="@string/button"
                    android:focusable="true">
            </RelativeLayout>
            
            <!-- styles.xml -->
            <style name="ButtonStyle" parent="MyAppTheme">
                <item name="android:scaleType">centerInside</item>
                <item name="android:adjustViewBounds">true</item>
                <item name="colorControlHighlight">@color/buttonColorPressed</item>
                <item name="colorButtonNormal">@color/buttonColor</item>
            </style>
            
            <!-- v21/styles.xml -->
            <style name="TargetButton" parent="MyAppTheme">
                <item name="android:scaleType">centerInside</item>
                <item name="android:adjustViewBounds">true</item>
                <item name="colorControlHighlight">@color/buttonColorPressed</item>
                <item name="colorButtonNormal">@color/buttonColor</item>
                <item name="android:elevation">@dimen/button_elevation</item>
            </style>
            
            <!-- drawable/button_selector.xml -->
            <selector xmlns:android="http://schemas.android.com/apk/res/android">
                <item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>
                <item android:drawable="@drawable/button"/>
            </selector>
            
            <!-- drawable/button.xml -->
            <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
                  android:src="@drawable/ic_button"
                  android:gravity="center" />
            
            <!-- drawable/button_pressed.xml -->
            <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
                  android:src="@drawable/ic_button_pressed"
                  android:gravity="center" />
            

            接下来,在 onCreate 中,当您检索按钮时,调用 adjustButtonSize(我把它放在我的基类中):

                mButton = (AppCompatImageButton) findViewById(R.id.button);
                adjustButtonSize(mButton);
            
            public void adjustButtonSize(AppCompatImageButton button) {
                DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
                int width = displayMetrics.widthPixels;
                int height = displayMetrics.heightPixels;
                ViewGroup.LayoutParams params = button.getLayoutParams();
                params.height = height / 10;         // 10%
                params.width = ((width * 20) / 100); // 20%
                button.setLayoutParams(params);
            }
            

            【讨论】:

              【解决方案7】:

              尝试只使用mMenuContainer.addView(button);

              【讨论】:

                【解决方案8】:

                你可以试试这个 kotlin 代码。

                当您想将另一个视图的大小赋予另一个视图时,这非常有用。

                class MainActivity : AppCompatActivity() {
                
                    override fun onCreate(savedInstanceState: Bundle?) {
                        super.onCreate(savedInstanceState)
                        setContentView(R.layout.activity_main)
                
                        equalToButton.post(Runnable { zeroButton.height = equalToButton.height })
                    }
                }
                

                【讨论】:

                  猜你喜欢
                  • 2011-08-31
                  • 2013-01-13
                  • 2015-07-31
                  • 2017-02-28
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-11-21
                  • 1970-01-01
                  相关资源
                  最近更新 更多