以编程方式更改android中按钮的宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11293932/
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
Programmatically change the width of the Button in android
提问by Joyson
How to change the width
of a Button
programmatically. I am able to change the height
but the width does not change. Following is my code snippet
如何以编程方式更改width
a 的Button
。我可以改变height
但宽度不会改变。以下是我的代码片段
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);
}
But the width
of the Button
occupies the width
of the screen.
但width
的Button
占据了width
屏幕。
回答by MKJParekh
button.setLayoutParams(new LinearLayout.LayoutParams(10, 100));
button.setLayoutParams(new LinearLayout.LayoutParams(10, 100));
Should have work for you.
应该为你工作。
There is one Awesome Example online for this : check this
网上有一个很棒的例子:检查这个
you should apply LayoutParams
of the ViewGroup
that contains your View
. That is, if your button is inside RelativeLayout
, you should use RelativeLayout.LayoutParams
你应该申请LayoutParams
的ViewGroup
包含您的View
。也就是说,如果你的按钮在里面RelativeLayout
,你应该使用RelativeLayout.LayoutParams
回答by Aqif Hamid
you can do it this way.
你可以这样做。
LinearLayout.LayoutParams params = button.getLayoutParams();
params.width = 100;
button.setLayoutParams(params);
but make sure you do this, after adding your button to a parent. and LinearLayout.LayoutParams
is used when parent is a LinearLayout
and when parent is RelativeLayout
use RelativeLayout.LayoutParams
.
但请确保在将按钮添加到父级后执行此操作。andLinearLayout.LayoutParams
当 parent 是 aLinearLayout
并且 parent 是RelativeLayout
use时使用RelativeLayout.LayoutParams
。
回答by Davide Maccarrone
I solved by using this:
我用这个解决了:
//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;
I hope this help.
我希望这会有所帮助。
回答by Lee Hounshell
In my case I am using AppCompatImageButton (and AppCompat). It turns out that if I keep density adjusted images in the various drawable folders: drawable-ldpi, drawable-mdpi, drawable-hdpi, etc.. then the button won't scale. Instead I had to put a single drawable for each button in drawable-nodpi. Make sure the drawable-nodpi button image is of the largestsize you need. Android will not scale the button size up; however, it can scale the size down. I created a button in XML like this:
就我而言,我使用的是 AppCompatImageButton(和 AppCompat)。事实证明,如果我将密度调整后的图像保存在各种可绘制文件夹中:drawable-ldpi、drawable-mdpi、drawable-hdpi 等,那么按钮将不会缩放。相反,我不得不在 drawable-nodpi 中为每个按钮放置一个 drawable。确保 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" />
Next, in onCreate when you retrieve the button, call adjustButtonSize(which i put in my base class):
接下来,当您检索按钮时,在 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);
}
回答by INZIXI
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) yourButton.getLayoutParams();
layoutParams.width = 150;
yourButton.setLayoutParams(layoutParams);
the "ConstraintLayout" can change up to your layout and set size by "layoutParams.width". This case it's work for me!
“ConstraintLayout”可以更改为您的布局并通过“layoutParams.width”设置大小。这个案例对我有用!
回答by Cícero Moura
can do this
可以这样做
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);
回答by Niko
Try using only mMenuContainer.addView(button);
尝试仅使用 mMenuContainer.addView(button);
回答by Anirudha Mahale
You can try out this kotlin code.
你可以试试这个 kotlin 代码。
This is great when you want to give the size of another view to some other view.
当您想将另一个视图的大小赋予其他视图时,这非常有用。
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
equalToButton.post(Runnable { zeroButton.height = equalToButton.height })
}
}