Android 布局讲解

本文通过一个简单的例子,来讲解Android Activity布局

 

首先让我们看看如下实例代码:

import android.app.Activity; import android.os.Bundle; import android.view.ViewGroup.LayoutParams; import android.widget.LinearLayout; import android.widget.TextView; public class HelloWorld extends Activity { // TextView that will be assigned to the // TextView resource when it's inflated or // created in code. TextView myTextView; // Variable used to determine if the layout // should be inflated from XML or constructed // in code. private static boolean inflate = true; /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); if (inflate) inflateXMLLayout(); else constructLayout(); } // Use the 'main.xml' layout resource to create // the UI for this Activity. private void inflateXMLLayout() { setContentView(R.layout.main); myTextView = (TextView)findViewById(R.id.myTextView); } // Create the Activity's UI layout by creating // and populating the layout in code. private void constructLayout() { LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); LinearLayout.LayoutParams textViewLP = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); myTextView = new TextView(this); myTextView.setText("Hello World, HelloWorld"); ll.addView(myTextView, textViewLP); addContentView(ll, lp); } }

 

这里用到了main.xml文件,代码如下:

 

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/myTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello World, HelloWorld" /> </LinearLayout>

你可能感兴趣的:(android,UI,layout,Class,import,encoding)