版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangxuewei111/article/details/44966513
1.LinearLayout线性布局
activity_main.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<<strong>LinearLayout</strong> xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<!--
vertical:垂直方向上排列, 内部控件的 高度 绝对不能设置为match_parent,因为会独占一列
horizontal:水平方向上排列,内部控件的 宽度 绝对不能设置为match_parent,因为会独占一行
-->
<!-- <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<strong>android:layout_gravity="top"</strong>
android:text="Button1"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<strong>android:layout_gravity="center_vertical"</strong>
android:text="Button2"
/>
</span><pre name="code" class="html"><span style="font-size:18px;"><!--
android:layout_gravity:用于指控件在布局中的对齐方式</span>
<span style="font-size:18px;"><span style="white-space:pre"> </span>android:gravity:用于指定文字在控件中的对齐方式
--></span>
<Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom" android:text="Button3" /> --> <EditText android:id="@+id/input_message" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Type something" />
<span style="font-size:18px;"><!--由于我们使用了android:layout_weight属性,此时控件的宽度就不应该再由android:layout_width来决定</span>
<span style="font-size:18px;"><span style="white-space:pre"> </span>,这里指定成0是一种比较规范的写法</span>
<span style="font-size:18px;">-->
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
<strong>android:layout_weight="1"</strong>
android:text="Button4"
/>
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button5"
/>
</<strong>LinearLayout</strong>></span>
2.RelativeLayout相对布局
relative_activity.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<<strong>RelativeLayout </strong>xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/relbutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="左上"
/>
<Button
android:id="@+id/relbutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="右上"
/>
<Button
android:id="@+id/relbutton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="右下"
/>
<Button
android:id="@+id/relbutton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="左下"
/>
<Button
android:id="@+id/relbutton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="中间"
/>
<Button
android:id="@+id/relbutton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/relbutton5"
android:layout_toLeftOf="@id/relbutton5"
android:text="按钮左上"
/>
<Button
android:id="@+id/relbutton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/relbutton5"
android:layout_toRightOf="@id/relbutton5"
android:text="按钮右上"
/>
<Button
android:id="@+id/relbutton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/relbutton5"
android:layout_toRightOf="@id/relbutton5"
android:text="按钮右下"
/>
<Button
android:id="@+id/relbutton9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/relbutton5"
android:layout_toLeftOf="@id/relbutton5"
android:text="按钮左下"
/>
</<strong>RelativeLayout</strong>></span>
3.FrameLayout
frame_activity.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<<strong>FrameLayout </strong>xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/frbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
/>
<ImageView
android:id="@+id/frimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
</<strong>FrameLayout</strong>></span>
4.TableLayout
table_activity.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<<strong>TableLayout </strong>xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1"
>
<!--android:stretchColumns="1":指定为1拉伸第二列 -->
<TableRow >
<TextView
android:layout_height="wrap_content"
android:text="Account:"
/>
<EditText
android:id="@+id/account"
android:layout_height="wrap_content"
android:hint="input your account"
/>
</TableRow>
<TableRow >
<TextView
android:layout_height="wrap_content"
android:text="Password:"
/>
<EditText
android:id="@+id/Password"
android:layout_height="wrap_content"
android:hint="password"
/>
</TableRow>
<!-- android:layout_span="2":让登录按钮占据两列 -->
<TableRow >
<Button
android:id="@+id/login"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="login"
/>
</TableRow>
</<strong>TableLayout</strong>></span>
MainActivity.java
package com.example.uilayouttest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button4);
/*
Button button = (Button) findViewById(R.id.button5);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//RelativeLayout
// Intent intent = new Intent(MainActivity.this,SecondActivity.class);
// startActivity(intent);
//FrameLayout
// Intent intent = new Intent(MainActivity.this,ThirdActivity.class);
// startActivity(intent);
//TableLayout
Intent intent = new Intent(MainActivity.this,ForthActivity.class);
startActivity(intent);
}
});*/
}
}
SecondActivity.java
package com.example.uilayouttest;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.relative_activity);
}
}
ThirdActivity.java
package com.example.uilayouttest;
import android.app.Activity;
import android.os.Bundle;
public class ThirdActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.frame_activity);
}
}
ForthActivity.java
package com.example.uilayouttest;
import android.app.Activity;
import android.os.Bundle;
public class ForthActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table_activity);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.uilayouttest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name=".ThirdActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name=".ForthActivity"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
代码地址:http://download.csdn.net/detail/wangxuewei111/8579523