Android 学习之Layout
学习Android已经一个星期了,,进度有点慢,继续坚持
1.主界面:
1 package com.wention.Layout; 2 3 import com.wention.Layout.R; 4 5 import android.app.Activity; 6 import android.content.Intent; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 12 public class MainActivity extends Activity { 13 /** 14 * button1-->listener1:LinerLayout 15 * button2-->listener2:FrameLayout 16 * button3-->listener3:RelativeLayout 17 * button4-->listener4:AbsoluteLayout 18 * button5-->listener5:TableLayout 19 */ 20 private Button button1; 21 private Button button2; 22 private Button button3; 23 private Button button4; 24 private Button button5; 25 26 /** Called when the activity is first created. */ 27 @Override 28 public void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 setContentView(R.layout.main); 31 32 OnClickListener listener1 = new OnClickListener() { 33 public void onClick(View v) { 34 Intent intent1 = new Intent(MainActivity.this, LinearLayoutActivity.class); 35 setTitle("LinearLayout"); 36 startActivity(intent1); 37 } 38 }; 39 OnClickListener listener2 = new OnClickListener() { 40 public void onClick(View v) { 41 Intent intent2 = new Intent(MainActivity.this, FrameLayoutActivity.class); 42 setTitle("FrameLayout"); 43 startActivity(intent2); 44 } 45 }; 46 OnClickListener listener3 = new OnClickListener() { 47 public void onClick(View v) { 48 Intent intent3 = new Intent(MainActivity.this, RelativeLayoutActivity.class); 49 setTitle("RelativeLayout"); 50 startActivity(intent3); 51 } 52 }; 53 OnClickListener listener4 = new OnClickListener() { 54 public void onClick(View v) { 55 Intent intent4 = new Intent(MainActivity.this, AbsoluteLayoutActivity.class); 56 setTitle("AbsoluteLayout"); 57 startActivity(intent4); 58 } 59 }; 60 OnClickListener listener5 = new OnClickListener() { 61 public void onClick(View v) { 62 Intent intent5 = new Intent(MainActivity.this, TableLayoutActivity.class); 63 setTitle("TableLayout"); 64 startActivity(intent5); 65 } 66 }; 67 68 button1 = (Button) findViewById(R.id.button1); 69 button1.setOnClickListener(listener1); 70 button2 = (Button) findViewById(R.id.button2); 71 button2.setOnClickListener(listener2); 72 button3 = (Button) findViewById(R.id.button3); 73 button3.setOnClickListener(listener3); 74 button4 = (Button) findViewById(R.id.button4); 75 button4.setOnClickListener(listener4); 76 button5 = (Button) findViewById(R.id.button5); 77 button5.setOnClickListener(listener5); 78 79 } 80 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <Button 8 android:id="@+id/button1" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:text="@string/buttonName01" /> 12 13 <Button 14 android:id="@+id/button2" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:text="@string/buttonName02" /> 18 19 <Button 20 android:id="@+id/button3" 21 android:layout_width="fill_parent" 22 android:layout_height="wrap_content" 23 android:text="@string/buttonName03" /> 24 25 <Button 26 android:id="@+id/button4" 27 android:layout_width="fill_parent" 28 android:layout_height="wrap_content" 29 android:text="@string/buttonName04" /> 30 31 <Button 32 android:id="@+id/button5" 33 android:layout_width="fill_parent" 34 android:layout_height="wrap_content" 35 android:text="@string/buttonName05" /> 36 37 </LinearLayout>
2.LinearLayout:
1 package com.wention.Layout; 2 3 import com.wention.Layout.R; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 8 public class LinearLayoutActivity extends Activity { 9 public void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.linear_layout); 12 } 13 14 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <LinearLayout 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:layout_weight="1" 11 android:orientation="vertical" > 12 13 <TextView 14 android:id="@+id/textView1" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 android:background="@color/red" 18 android:gravity="center" 19 android:layout_weight="1" 20 android:text="@string/rowName01" /> 21 22 <TextView 23 android:id="@+id/textView2" 24 android:layout_width="fill_parent" 25 android:layout_height="wrap_content" 26 android:background="@color/green" 27 android:gravity="center" 28 android:layout_weight="1" 29 android:text="@string/rowName02" /> 30 31 <TextView 32 android:id="@+id/textView3" 33 android:layout_width="fill_parent" 34 android:layout_height="wrap_content" 35 android:background="@color/blue" 36 android:gravity="center" 37 android:layout_weight="1" 38 android:text="@string/rowName03" /> 39 40 <TextView 41 android:id="@+id/textView4" 42 android:layout_width="fill_parent" 43 android:layout_height="wrap_content" 44 android:background="@color/yellow" 45 android:gravity="center" 46 android:layout_weight="1" 47 android:text="@string/rowName04" /> 48 49 </LinearLayout> 50 51 <LinearLayout 52 android:layout_width="fill_parent" 53 android:layout_height="wrap_content" 54 android:layout_weight="1" 55 android:orientation="horizontal" > 56 57 <TextView 58 android:id="@+id/textView5" 59 android:layout_width="wrap_content" 60 android:layout_height="fill_parent" 61 android:background="@color/red" 62 android:gravity="center" 63 android:layout_weight="1" 64 android:text="@string/lineName01" /> 65 66 <TextView 67 android:id="@+id/textView6" 68 android:layout_width="wrap_content" 69 android:layout_height="fill_parent" 70 android:background="@color/green" 71 android:gravity="center" 72 android:layout_weight="1" 73 android:text="@string/lineName02" /> 74 75 <TextView 76 android:id="@+id/textView7" 77 android:layout_width="wrap_content" 78 android:layout_height="fill_parent" 79 android:background="@color/blue" 80 android:gravity="center" 81 android:layout_weight="1" 82 android:text="@string/lineName03" /> 83 84 <TextView 85 android:id="@+id/textView8" 86 android:layout_width="wrap_content" 87 android:layout_height="fill_parent" 88 android:background="@color/orange" 89 android:gravity="center" 90 android:layout_weight="1" 91 android:text="@string/lineName04" /> 92 93 </LinearLayout> 94 95 </LinearLayout>
3.FrameLayout:
1 package com.wention.Layout; 2 3 import com.wention.Layout.R; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 8 public class FrameLayoutActivity extends Activity { 9 public void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.frame_layout); 12 } 13 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8"?> 2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <ImageView 7 android:id="@+id/imageView1" 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:src="@drawable/background" /> 11 12 <ImageView 13 android:id="@+id/imageView2" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:src="@drawable/flower" /> 17 18 <ImageView 19 android:id="@+id/imageView3" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:src="@drawable/life" /> 23 24 </FrameLayout>
4.RelativeLayout
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.wention.Layout; 2 3 import com.wention.Layout.R; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 8 public class RelativeLayoutActivity extends Activity { 9 public void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.relative_layout); 12 } 13 14 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <ImageView 7 android:id="@+id/imageView1" 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:layout_alignParentLeft="true" 11 android:src="@drawable/album" /> 12 13 <ImageButton 14 android:id="@+id/imageButton1" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:layout_alignParentRight="true" 18 android:src="@drawable/play" /> 19 20 <LinearLayout 21 android:id="@+id/linearLayout1" 22 android:layout_width="fill_parent" 23 android:layout_height="wrap_content" 24 android:layout_toLeftOf="@+id/imageButton1" 25 android:layout_toRightOf="@+id/imageView1" 26 android:orientation="vertical" > 27 28 <TextView 29 android:layout_width="fill_parent" 30 android:layout_height="wrap_content" 31 android:background="@color/yellow" 32 android:text="千纸鹤" 33 android:textColor="@color/blue" 34 android:textSize="20sp" /> 35 36 <TextView 37 android:layout_width="fill_parent" 38 android:layout_height="wrap_content" 39 android:background="@color/orange" 40 android:text="方大同" 41 android:textColor="@color/green" 42 android:textSize="12sp" /> 43 </LinearLayout> 44 45 46 <ImageView 47 android:id="@+id/imageView2" 48 android:layout_width="wrap_content" 49 android:layout_height="wrap_content" 50 android:layout_alignParentRight="true" 51 android:layout_below="@+id/imageView1" 52 android:layout_marginTop="34dp" 53 android:src="@drawable/lyric" /> 54 55 </RelativeLayout>
5.AbsoluteLayout
1 package com.wention.Layout; 2 3 import com.wention.Layout.R; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 8 public class AbsoluteLayoutActivity extends Activity { 9 public void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.absolute_layout); 12 } 13 14 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/widget1" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 android:background="@color/orange" > 7 8 <TextView 9 android:id="@+id/widget2" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:layout_x="61dp" 13 android:layout_y="69dp" 14 android:text="@string/qq" 15 android:textColor="@color/blue" /> 16 17 <TextView 18 android:id="@+id/widget3" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:layout_x="61dp" 22 android:layout_y="158dp" 23 android:text="@string/password" 24 android:textColor="@color/blue" 25 android:textSize="18sp" /> 26 27 <EditText 28 android:id="@+id/widget4" 29 android:layout_width="150dip" 30 android:layout_height="wrap_content" 31 android:layout_x="114dp" 32 android:layout_y="57dp" 33 android:inputType="number" 34 android:textColor="@color/red" 35 android:textSize="18sp" > 36 37 <requestFocus /> 38 </EditText> 39 40 <EditText 41 android:id="@+id/widget5" 42 android:layout_width="150dip" 43 android:layout_height="wrap_content" 44 android:layout_x="114dp" 45 android:layout_y="142dp" 46 android:inputType="textPassword" 47 android:textColor="@color/red" 48 android:textSize="18sp" /> 49 50 <Button 51 android:id="@+id/button1" 52 android:layout_width="120dip" 53 android:layout_height="wrap_content" 54 android:layout_x="20dp" 55 android:layout_y="210dp" 56 android:text="@string/login" /> 57 58 <Button 59 android:id="@+id/button2" 60 android:layout_width="120dip" 61 android:layout_height="wrap_content" 62 android:layout_x="180dp" 63 android:layout_y="210dp" 64 android:text="@string/quit" /> 65 66 67 </AbsoluteLayout>
6.TableLayout
1 package com.wention.Layout; 2 3 import com.wention.Layout.R; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 8 public class TableLayoutActivity extends Activity { 9 public void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.table_layout); 12 } 13 14 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:stretchColumns="1" > 6 7 <TableRow> 8 9 <TextView 10 android:gravity="right" 11 android:padding="3dip" 12 android:text="@string/qq" 13 android:textStyle="bold" /> 14 15 <EditText 16 android:id="@+id/username" 17 android:padding="3dip" 18 android:scrollHorizontally="true" /> 19 </TableRow> 20 21 <TableRow> 22 23 <TextView 24 android:gravity="right" 25 android:padding="3dip" 26 android:text="@string/password" 27 android:textStyle="bold" /> 28 29 <EditText 30 android:id="@+id/password" 31 android:padding="3dip" 32 android:password="true" 33 android:scrollHorizontally="true" /> 34 </TableRow> 35 36 <TableRow android:gravity="right" > 37 38 <Button 39 android:id="@+id/cancel" 40 android:text="@string/quit" /> 41 42 <Button 43 android:id="@+id/login" 44 android:text="@string/login" /> 45 </TableRow> 46 47 </TableLayout>
String.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 4 <string name="hello">Hello World, MainActivity!</string> 5 <string name="app_name">Layout</string> 6 <string name="buttonName01">LinerLayout</string> 7 <string name="buttonName02">FrameLayout</string> 8 <string name="buttonName03">RelativeLayout</string> 9 <string name="buttonName04">AbsoluteLayout</string> 10 <string name="buttonName05">TableLayout</string> 11 <string name="rowName01">第一行</string> 12 <string name="rowName02">第二行</string> 13 <string name="rowName03">第三行</string> 14 <string name="rowName04">第四行</string> 15 <string name="lineName01">第一列</string> 16 <string name="lineName02">第二列</string> 17 <string name="lineName03">第三列</string> 18 <string name="lineName04">第四列</string> 19 <string name="qq">QQ:</string> 20 <string name="password">密码:</string> 21 <string name="login">登录:</string> 22 <string name="quit">取消:</string> 23 24 </resources>
color.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 <color name="red">#ff0000</color> 4 <color name="green">#00ff00</color> 5 <color name="blue">#0000ff</color> 6 <color name="yellow">#ffcc00</color> 7 <color name="orange">#ff6600</color> 8 9 </resources>
源码:Layout.rar