2014-01-09 27 views
2

我每天都在使用Android,並且我想避免視圖大小的規範。 舉例來說,如果我做一個TextView,我指定高度和寬度屬性,像這樣:有沒有辦法避免在android佈局上的大小規格?

<TextView 
     android:id="@+id/activity_lbl" 
     android:layout_width="wrap_content" <!-- Again --> 
     android:layout_height="wrap_content" <!--Again... --> /> 

    <TextView 
     android:id="@+id/activity_lbl2" 
     android:layout_width="wrap_content" <!-- Again... --> 
     android:layout_height="wrap_content" <!--Again, for ever --> /> 

而對於我創建的每一個觀點,我到分配相同的規格尺寸。 有沒有辦法避免它們?

文件:RES /價值/ styles.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources xmlns:android="http://schemas.android.com/apk/res/android"> 
    <style name="wrapall"> 
    <item name="android:layout_width">wrap_content</item> 
    <item name="android:layout_height">wrap_content</item> 
    </style> 
</resources> 

然後將它應用到你的佈局,這樣

+0

從文檔的layout_width和layout_height:「指定視圖的基本寬度。這是包含佈局管理器內部任何視圖的必需屬性。「http://developer.android.com/reference/android/R.styleable.html – harun

回答

5

,你會在你的佈局文件都適用這樣的風格中聲明它:

<TextView 
     android:id="@+id/activity_lbl" 
     style="@style/wrapall" /> 

    <TextView 
     android:id="@+id/activity_lbl2" 
     style="@style/wrapall" /> 
+2

我會鼓勵這種方法,因爲您將來可能想要設置所有文本視圖的文本大小或顏色 – OrhanC1

+1

謝謝@keyboardsurfer,這將會非常有用 – Rafael

-2

你無法避免的

android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

,因爲它是重要的,但我想給你的,你可以在你的類中使用這個圖像某些佈局 一些代碼:

ImageView.getLayoutParams().height = YourHeightValue ; 
    ImageView.getLayoutParams().width = YourWidthValue; 

爲TextView的使用Textview.setSize(); or TextView.setwidth(int); TextView.setHeight(int);

+0

爲什麼要在Java中添加樣式屬性到XML中定義的東西? – OrhanC1

+0

對不起,我的意思是爲什麼要添加一個Java中的靜態(不變)屬性? – OrhanC1

相關問題