2012-07-02 74 views
31

如何以編程方式更改按鈕的寬度。我可以改變高度,但寬度不會改變。以下是我的代碼片段以編程方式更改android中按鈕的寬度

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


} 

但是按鈕的寬度佔用了屏幕的寬度。

回答

75

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

應該有適合你的工作。

有一個真棒實例在線這樣的:check this


你應該申請包含您ViewViewGroupLayoutParams。也就是說,如果你的按鈕是內部RelativeLayout,你應該使用RelativeLayout.LayoutParams

+3

重要的是要注意:你應該應用包含你的View的'ViewGroup'的'LayoutParams'。也就是說,如果你的按鈕在'RelativeLayout'裏面,你應該使用'RelativeLayout.LayoutParams'。 – agamov

+0

那麼,如何指定單位? –

+1

您應該將尺寸值乘以顯示比例,因爲構造函數需要像素而不是dp。 像這樣: widthInPixels = getResources()。getDisplayMetrics()。密度* widthInDp; –

31

你可以這樣做。

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

但確保在將按鈕添加到父項後執行此操作。當父母爲LinearLayout且父母爲RelativeLayout時,使用LinearLayout.LayoutParams,使用RelativeLayout.LayoutParams

+3

謝謝,解決了我的問題:D! – Bhimbim

0

嘗試使用只有mMenuContainer.addView(button);

1

能做到這一點

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

我用這個解決:

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

我希望這幫助。

0

在我的情況下,我使用AppCompatImageButton(和AppCompat)。 事實證明,如果我將密度調整圖像保存在各種可繪製的 文件夾中:drawable-ldpi,drawable-mdpi,drawable-hdpi等,那麼按鈕將不會縮放。 相反,我不得不爲放置一個drawable-nodpi中的每個按鈕。 確保可繪製的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); 
} 
相關問題