2012-10-11 73 views
0

我想設置我的LinearLayout代碼端的寬度。我試圖找到一種方法,我可以將我的IcsSpinner的selectedNavigationItem大小設置爲選定的項目大小...當我將xLineLayout設置爲特定寬度時,實現此操作...但我需要動態調整大小。以編程方式設置在XML中聲明的LinearLayout的寬度

<LinearLayout 
     android:id="@+id/spinner_ll" 
     android:layout_width="75dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" > 

     <com.actionbarsherlock.internal.widget.IcsSpinner 
      android:id="@+id/spinner" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      /> 
    </LinearLayout> 

是否有任何方法來設置LinearLayout的寬度,而不是孩子?

UPDATE ...回答:

LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)ll.getLayoutParams(); 
lp.width = 85; // set this to size...this is arbitrary number for proof of concept 
ll.requestLayout(); 

回答

0

您可以從代碼中設置的佈局參數是這樣的:

LinearLayout layout = (LinearLayout) findViewById(R.id.spinner_11); 
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100 /* in pixels */, LayoutParams.WRAP_CONTENT); 
layout.setLayoutParams(layoutParams); 

可以DPS轉換爲像素在這樣的代碼:

int pixelValue = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10 /* in dps */, getResources().getDisplayMetrics()); 

看看這裏的文檔:https://developer.android.com/reference/android/util/TypedValue.html#applyDimension(int,float,android.util.DisplayMetrics)

+0

這幾乎是正確的我的情況....我需要保存其他佈局參數在我的XML。因此,我需要這樣做:LinearLayout.LayoutParams lp =(LinearLayout.LayoutParams)ll.getLayoutParams(); \t \t lp.width = 85; \t \t ll.requestLayout(); – taraloca

+0

當然,你也可以做到這一點,我只是保持簡單:) –

相關問題