控件的布局,可以再java代码中动态的设置,也可以在布局文件中设置(比较死板)
1 import android.os.Bundle; 2 import android.app.Activity; 3 4 public class Layout01 extends Activity { 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.main); 9 } 10 }
布局文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <!-- 8 android:orientation:布局的方向,线性布局才有 9 android:id —— 为控件指定相应的ID 10 android:text —— 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串 11 android:grivity —— 指定控件的基本位置,比如说居中,居右等位置(控件里面的内容位置) 12 android:textSize —— 指定控件当中字体的大小 13 android:background —— 指定该控件所使用的背景色,RGB命名法 14 android:width —— 指定控件的宽度 15 android:height —— 指定控件的高度 16 android:padding* —— 指定控件的内边距,也就是说控件当中的内容 17 android:layout_margin*——指定控件的外边距,也就是说控件跟父控件之间的距离 18 android:sigleLine —— 如果设置为真的话,则将控件的内容在同一行当中进行显示 19 --> 20 <TextView 21 android:id="@+id/firstText" 22 android:text="第一行" 23 android:gravity="center_vertical" 24 android:textSize="15pt" 25 android:background="#aa0000" 26 android:layout_width="fill_parent" 27 android:layout_height="wrap_content" 28 android:paddingLeft="10dip" 29 android:paddingTop="20dip" 30 android:paddingRight="30dip" 31 android:paddingBottom="40dip" 32 android:layout_weight="1" 33 android:singleLine="true"/> 34 <TextView 35 android:id="@+id/secondText" 36 android:text="第二行" 37 android:gravity="center_horizontal" 38 android:textSize="15pt" 39 android:background="#0000aa" 40 android:layout_width="fill_parent" 41 android:layout_height="wrap_content" 42 android:padding="40dip" 43 android:layout_weight="1"/> 44 </LinearLayout>
清单文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.mars.layout01" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="4" 9 android:targetSdkVersion="18" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name="com.mars.layout01.Layout01" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 </application> 26 27 </manifest>
string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Layout01</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> </resources>
控件布局_LinearLayout,布布扣,bubuko.com
原文:http://www.cnblogs.com/LO-ME/p/3584499.html