[英]Programmatically change the width of the Button in android
How to change the width
of a Button
programmatically. 如何以编程方式更改Button
的width
。 I am able to change the height
but the width does not change. 我能够改变height
但宽度不会改变。 Following is my code snippet 以下是我的代码段
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
的画面。
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
. 您应该应用包含View
的ViewGroup
LayoutParams
。 That is, if your button is inside RelativeLayout
, you should use RelativeLayout.LayoutParams
也就是说,如果您的按钮位于RelativeLayout
,则应使用RelativeLayout.LayoutParams
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
. 和LinearLayout.LayoutParams
当母体是一种用于LinearLayout
,并且当父RelativeLayout
使用RelativeLayout.LayoutParams
。
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. 我希望这有帮助。
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);
In my case I am using AppCompatImageButton (and AppCompat). 在我的情况下,我使用AppCompatImageButton(和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. 事实证明,如果我在各种可绘制文件夹中保持密度调整图像:drawable-ldpi,drawable-mdpi,drawable-hdpi等,那么按钮将无法缩放。 Instead I had to put a single drawable for each button in drawable-nodpi . 相反,我必须为drawable-nodpi中的每个按钮添加一个drawable 。 Make sure the drawable-nodpi button image is of the largest size you need. 确保drawable-nodpi按钮图像是您需要的最大尺寸。 Android will not scale the button size up; Android不会缩放按钮大小; however, it can scale the size down. 但是,它可以缩小尺寸。 I created a button in XML like this: 我在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);
}
尝试仅使用mMenuContainer.addView(button);
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 })
}
}
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". “ConstraintLayout”可以通过“layoutParams.width”更改为您的布局和设置大小。 This case it's work for me! 这个案子对我有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.