Android中的LayoutInflater
什麼用? java
這是另外一個與前一個相似的示例,但進一步展現了它能夠提供的膨脹參數和動態行爲。 android
假設您的ListView行佈局能夠包含可變數量的TextView。 因此首先你膨脹基礎項View(就像前面的例子同樣),而後在運行時循環動態添加TextViews。 使用android:layout_weight還能夠完美地對齊全部內容。 ide
如下是Layouts資源: 佈局
list_layout.xml this
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/field1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2"/> <TextView android:id="@+id/field2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout>
schedule_layout.xml spa
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"/>
在BaseAdapter類的擴展中覆蓋getView方法 code
@Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = activity.getLayoutInflater(); View lst_item_view = inflater.inflate(R.layout.list_layout, null); TextView t1 = (TextView) lst_item_view.findViewById(R.id.field1); TextView t2 = (TextView) lst_item_view.findViewById(R.id.field2); t1.setText("some value"); t2.setText("another value"); // dinamically add TextViews for each item in ArrayList list_schedule for(int i = 0; i < list_schedule.size(); i++){ View schedule_view = inflater.inflate(R.layout.schedule_layout, (ViewGroup) lst_item_view, false); ((TextView)schedule_view).setText(list_schedule.get(i)); ((ViewGroup) lst_item_view).addView(schedule_view); } return lst_item_view; }
注意不一樣的inflate方法調用: xml
inflater.inflate(R.layout.list_layout, null); // no parent inflater.inflate(R.layout.schedule_layout, (ViewGroup) lst_item_view, false); // with parent preserving LayoutParams
這裏是一個示例,用於爲佈局的根視圖建立一個引用,對其進行膨脹並將其與setContentView(View視圖)一塊兒使用 對象
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LayoutInflater li=getLayoutInflater(); View rootView=li.inflate(R.layout.activity_main,null); setContentView(rootView); }
LayoutInflater.inflate()提供了一種方法,用於將定義視圖的res / layout / * .xml文件轉換爲可在應用程序源代碼中使用的實際View對象。 ip
基本的兩個步驟:獲取inflater而後膨脹資源
你怎麼獲得inflater?
LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
假設xml文件是「list_item.xml」,你如何獲得視圖?
View view = inflater.inflate(R.layout.list_item, parent, false);
膨脹意味着讀取描述佈局(或GUI元素)的XML文件並建立與其對應的實際對象,從而使對象在Android應用程序中可見。
final Dialog mDateTimeDialog = new Dialog(MainActivity.this); // Inflate the root layout final RelativeLayout mDateTimeDialogView = (RelativeLayout) getLayoutInflater().inflate(R.layout.date_time_dialog, null); // Grab widget instance final DateTimePicker mDateTimePicker = (DateTimePicker) mDateTimeDialogView.findViewById(R.id.DateTimePicker);
該文件能夠保存爲date_time_dialog.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/DateTimeDialog" android:layout_width="100px" android:layout_height="wrap_content"> <com.dt.datetimepicker.DateTimePicker android:id="@+id/DateTimePicker" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <LinearLayout android:id="@+id/ControlButtons" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/DateTimePicker" android:padding="5dip"> <Button android:id="@+id/SetDateTime" android:layout_width="0dip" android:text="@android:string/ok" android:layout_weight="1" android:layout_height="wrap_content" /> <Button android:id="@+id/ResetDateTime" android:layout_width="0dip" android:text="Reset" android:layout_weight="1" android:layout_height="wrap_content" /> <Button android:id="@+id/CancelDialog" android:layout_width="0dip" android:text="@android:string/cancel" android:layout_weight="1" android:layout_height="wrap_content" /> </LinearLayout>
此文件能夠保存爲date_time_picker.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" `enter code here` android:padding="5dip" android:id="@+id/DateTimePicker"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:baselineAligned="true" android:orientation="horizontal"> <LinearLayout android:id="@+id/month_container" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="1dp" android:layout_marginTop="5dp" android:layout_marginRight="5dp" android:layout_marginBottom="5dp" android:gravity="center" android:orientation="vertical"> <Button android:id="@+id/month_plus" android:layout_width="45dp" android:layout_height="45dp" android:background="@drawable/image_button_up_final"/> <EditText android:id="@+id/month_display" android:layout_width="45dp" android:layout_height="35dp" android:background="@drawable/picker_middle" android:focusable="false" android:gravity="center" android:singleLine="true" android:textColor="#000000"> </EditText> <Button android:id="@+id/month_minus" android:layout_width="45dp" android:layout_height="45dp" android:background="@drawable/image_button_down_final"/> </LinearLayout> <LinearLayout android:id="@+id/date_container" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="0.5dp" android:layout_marginTop="5dp" android:layout_marginRight="5dp" android:layout_marginBottom="5dp" android:gravity="center" android:orientation="vertical"> <Button android:id="@+id/date_plus" android:layout_width="45dp" android:layout_height="45dp" android:background="@drawable/image_button_up_final"/> <EditText android:id="@+id/date_display" android:layout_width="45dp" android:layout_height="35dp" android:background="@drawable/picker_middle" android:gravity="center" android:focusable="false" android:inputType="number" android:textColor="#000000" android:singleLine="true"/> <Button android:id="@+id/date_minus" android:layout_width="45dp" android:layout_height="45dp" android:background="@drawable/image_button_down_final"/> </LinearLayout> <LinearLayout android:id="@+id/year_container" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="0.5dp" android:layout_marginTop="5dp" android:layout_marginRight="5dp" android:layout_marginBottom="5dp" android:gravity="center" android:orientation="vertical"> <Button android:id="@+id/year_plus" android:layout_width="45dp" android:layout_height="45dp" android:background="@drawable/image_button_up_final"/> <EditText android:id="@+id/year_display" android:layout_width="45dp" android:layout_height="35dp" android:background="@drawable/picker_middle" android:gravity="center" android:focusable="false" android:inputType="number" android:textColor="#000000" android:singleLine="true"/> <Button android:id="@+id/year_minus" android:layout_width="45dp" android:layout_height="45dp" android:background="@drawable/image_button_down_final"/> </LinearLayout> <LinearLayout android:id="@+id/hour_container" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:gravity="center" android:orientation="vertical"> <Button android:id="@+id/hour_plus" android:layout_width="45dp" android:layout_height="45dp" android:background="@drawable/image_button_up_final"/> <EditText android:id="@+id/hour_display" android:layout_width="45dp" android:layout_height="35dp" android:background="@drawable/picker_middle" android:gravity="center" android:focusable="false" android:inputType="number" android:textColor="#000000" android:singleLine="true"> </EditText> <Button android:id="@+id/hour_minus" android:layout_width="45dp" android:layout_height="45dp" android:background="@drawable/image_button_down_final"/> </LinearLayout> <LinearLayout android:id="@+id/min_container" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="0.35dp" android:layout_marginTop="5dp" android:layout_marginRight="5dp" android:layout_marginBottom="5dp" android:gravity="center" android:orientation="vertical"> <Button android:id="@+id/min_plus" android:layout_width="45dp" android:layout_height="45dp" android:background="@drawable/image_button_up_final"/> <EditText android:id="@+id/min_display" android:layout_width="45dp" android:layout_height="35dp" android:background="@drawable/picker_middle" android:gravity="center" android:focusable="false" android:inputType="number" android:textColor="#000000" android:singleLine="true"/> <Button android:id="@+id/min_minus" android:layout_width="45dp" android:layout_height="45dp" android:background="@drawable/image_button_down_final"/> </LinearLayout> <LinearLayout android:id="@+id/meridiem_container" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="0.35dp" android:layout_marginTop="5dp" android:layout_marginRight="5dp" android:layout_marginBottom="5dp" android:gravity="center" android:orientation="vertical"> <ToggleButton android:id="@+id/toggle_display" style="@style/SpecialToggleButton" android:layout_width="40dp" android:layout_height="32dp" android:layout_marginLeft="5dp" android:layout_marginTop="45dp" android:layout_marginRight="5dp" android:layout_marginBottom="5dp" android:padding="5dp" android:gravity="center" android:textOn="@string/meridiem_AM" android:textOff="@string/meridiem_PM" android:checked="true"/> <!-- android:checked="true" --> </LinearLayout> </LinearLayout> </RelativeLayout>
MainActivity
類保存爲MainActivity.java :
public class MainActivity extends Activity { EditText editText; Button button_click; public static Activity me = null; String meridiem; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText)findViewById(R.id.edittext1); button_click = (Button)findViewById(R.id.button1); button_click.setOnClickListener(new OnClickListener() { @Override public void onClick(View view){ final Dialog mDateTimeDialog = new Dialog(MainActivity.this); final RelativeLayout mDateTimeDialogView = (RelativeLayout) getLayoutInflater().inflate(R.layout.date_time_dialog, null); final DateTimePicker mDateTimePicker = (DateTimePicker) mDateTimeDialogView.findViewById(R.id.DateTimePicker); // mDateTimePicker.setDateChangedListener(); ((Button) mDateTimeDialogView.findViewById(R.id.SetDateTime)).setOnClickListener(new OnClickListener() { public void onClick(View v) { mDateTimePicker.clearFocus(); int hour = mDateTimePicker.getHour(); String result_string = mDateTimePicker.getMonth() +" "+ String.valueOf(mDateTimePicker.getDay()) + ", " + String.valueOf(mDateTimePicker.getYear()) + " " +(mDateTimePicker.getHour()<=9? String.valueOf("0"+mDateTimePicker.getHour()) : String.valueOf(mDateTimePicker.getHour())) + ":" + (mDateTimePicker.getMinute()<=9?String.valueOf("0"+mDateTimePicker.getMinute()):String.valueOf(mDateTimePicker.getMinute()))+" "+mDateTimePicker.getMeridiem(); editText.setText(result_string); mDateTimeDialog.dismiss(); } }); // Cancel the dialog when the "Cancel" button is clicked ((Button) mDateTimeDialogView.findViewById(R.id.CancelDialog)).setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub mDateTimeDialog.cancel(); } }); // Reset Date and Time pickers when the "Reset" button is clicked ((Button) mDateTimeDialogView.findViewById(R.id.ResetDateTime)).setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub mDateTimePicker.reset(); } }); // Setup TimePicker // No title on the dialog window mDateTimeDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // Set the dialog content view mDateTimeDialog.setContentView(mDateTimeDialogView); // Display the dialog mDateTimeDialog.show(); } }); } }
個人自定義列表但願它說明概念
public class second extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.second); // TextView textview=(TextView)findViewById(R.id.textView1); // textview.setText(getIntent().getExtras().getString("value")); setListAdapter(new MyAdapter(this,R.layout.list_item,R.id.textView1, getResources().getStringArray(R.array.counteries))); } private class MyAdapter extends ArrayAdapter<String>{ public MyAdapter(Context context, int resource, int textViewResourceId, String[] objects) { super(context, resource, textViewResourceId, objects); // TODO Auto-generated constructor stub } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View row=inflater.inflate(R.layout.list_item,parent,false); String[]items=getResources().getStringArray(R.array.counteries); ImageView iv=(ImageView) row.findViewById(R.id.imageView1); TextView tv=(TextView) row.findViewById(R.id.textView1); tv.setText(items[position]); if(items[position].equals("unitedstates")){ iv.setImageResource(R.drawable.usa); }else if(items[position].equals("Russia")){ iv.setImageResource(R.drawable.russia); }else if(items[position].equals("Japan")){ iv.setImageResource(R.drawable.japan); } // TODO Auto-generated method stub return row; } } }