2012-07-07 127 views
0

其實我需要通過代碼來減少微調的字體大小,下面是我的代碼:如何減少代碼中的微調字體大小?

Spinner spiner2 = (Spinner) findViewById(R.id.Spinner2); 
    List<String> list = new ArrayList<String>(); 
    list.add("list 1"); 
    list.add("list 2"); 
    list.add("list 3"); 
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.trytry,list); 
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spiner2.setAdapter(dataAdapter); 

我創建了一個名爲作爲trytry.xml一個XML文件中ArrayAdapter提,但在我的setContentView提到的setContentView(R。 layout.main);我很困惑該怎麼辦呢

我trytry XML包含:測試規模android:textSize="18sp"

使用此代碼

<?xml version="1.0" encoding="utf-8"?> 
<Spinner xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:text="@+id/TextView01"  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="18dp"> 
</TextView> 
</Spinner> 
+1

'ArrayAdapter的DataAdapter =新ArrayAdapter(此,R .layout.YOURLAYOUT,list);' – 2012-07-07 12:27:17

+0

R.layout.YOURLAYOUT wat tat mean ,,如果setContentView(R.layout.main);但我的佈局xml文件是trytry.xml – 2012-07-07 12:29:20

+0

Main是整個屏幕的佈局,另一個是包含在主屏幕中的旋鈕的佈局。 – Barak 2012-07-07 12:31:43

回答

0

使用sp

public class AndroidCustomSpinner extends Activity { 

    String[] DayOfWeek = {"Sunday", "Monday", "Tuesday", 
       "Wednesday", "Thursday", "Friday", "Saturday"}; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Spinner mySpinner = (Spinner)findViewById(R.id.spinner); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
          R.layout.row, R.id.weekofday, DayOfWeek); 
     mySpinner.setAdapter(adapter); 
    } 
} 

row.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/weekofday" 
     android:layout_width="wrap_content" 
     android:textSize="18sp 
     android:layout_height="wrap_content"/> 
</LinearLayout> 
+0

我試過18sp現在,即時獲取強制關閉錯誤。如果我調試它運行整個代碼(直到最後一行),在logcat它顯示很多errors.how找到確切的錯誤? – 2012-07-07 12:36:12

+0

你正在得到什麼錯誤 – 2012-07-07 12:39:52

+0

07-07 18:12:25.002:E/AndroidRuntime(2182):未捕獲的處理程序:由於未捕獲的異常導致主線程退出 07-07 18:12:25.160:E/AndroidRuntime(2182)在android.widget.AdapterView.addView(AdapterView.java:461)中,AdapterView不支持java.lang.UnsupportedOperationException:addView(View,LayoutParams) 07-07 18:12:25.160:E/AndroidRuntime(2182):\t。在Android.view.LayoutInflater.rInflate(LayoutInflater.java:622) – 2012-07-07 12:43:01

0

有幾個問題正在進行。

首先,當您創建自己的佈局時,不應以android爲前綴。這是爲框架附帶的佈局保留的。對於你自己的佈局應該是R.id.trytry。其次,您需要將佈局設置爲關閉狀態(在初次調用適配器時)和下拉資源(如果希望它在打開時看起來相同)。

它應該是這個樣子:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);    // Set the layout for the whole screen 
       // Other code here 
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,R.layout.trytry,list); 
    dataAdapter.setDropDownViewResource(R.layout.trytry); 
} 

此外,您不能添加一個微調定義範圍內的視圖。這給你錯誤。

的微調標籤應該是您的main.xml中是這樣的:

<Spinner 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 

那麼你trytry.xml應該是這樣的:

<TextView  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:text="@+id/TextView01"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:textSize="18dp"/>