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
如何以编程方式更改按钮的宽度。我能够改变高度,但宽度不会改变。以下是我的代码段
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.
但是按钮的宽度占据了屏幕的宽度。
77
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
您应该应用包含View的ViewGroup的LayoutParams。也就是说,如果您的按钮位于RelativeLayout内,则应使用RelativeLayout.LayoutParams
32
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
.
但是在将按钮添加到父级之后,请确保执行此操作。当parent是LinearLayout时,使用LinearLayout.LayoutParams,当parent是RelativeLayout时,使用RelativeLayout.LayoutParams。
2
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.
我希望这有帮助。
1
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);
1
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 largest size 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);
}
0
Try using only mMenuContainer.addView(button);
尝试仅使用mMenuContainer.addView(按钮);
0
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 })
}
}
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2012/07/02/f20cdff6a53eaedb239c0c90d67a3982.html。