2013-04-05 38 views
0

我想獲得屏幕的寬度,然後更改按鈕的寬度。我正在使用setWidth,並且寬度沒有改變(我將它設置爲一個很小的數字,30,所以我可以判斷大小是否改變。 //這是行不通的,設置大小小,所以我真的可以告訴 mBut.setWidth(30); mBut.setOnClickListener(本);試圖改變一個按鈕的寬度

layouut

<LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:orientation="horizontal" > 

     <Button 
     android:id="@+id/butVol" 
     android:layout_width="100dp" 
     android:layout_height="wrap_content" 
     android:textSize="24px" 
     android:text="Volume" 
     android:textColor="#ff0000ff" 

    /> 

    <Button 
     android:id="@+id/butRington" 
     android:layout_width="100dp" 
     android:layout_height="wrap_content" 
     android:textSize="24px" 
     android:text="Rington" 
     android:textColor="#ff0000ff" 

    /> 

回答

0

檢索它掛layout_widht爲wrap_content後,我能夠寬度改變一下各種規模的,我需要。

<Button 
    android:id="@+id/butRington" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="R" 
    android:textColor="#ff0000ff" 
    android:textSize="24px" /> 
1

您需要修改按鈕的LayoutParams。

mBut.setLayoutParams(new LinearLayout.LayoutParams(30,xxxx));

其中xxx是按鈕的高度。您可以通過

ViewGroup.LayoutParams params = mBut.getLayoutParams(); 
params.height; 

檢查this post

0

獲取屏幕的寬度:

Display display = getWindowManager().getDefaultDisplay(); 
Point size = new Point(); 
display.getSize(size); 
int width = size.x 

現在你需要得到您的按鈕,然後設置新的寬度:

Button b = (Button) findViewById(R.id.button1); 
b.setWidth(width);