08-18 Activity跳轉 Button LinearLayout佈局 Relative屬性

Activity跳轉

![這裏寫圖片描述](http://img.blog.csdn.net/20150818205036745)
//**MainActivity**
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn=(Button)findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(),Second_Activity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("myApplication", "onStart ");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("myApplication", "onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("myApplication", "onResume ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("myApplication", "onPause ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("myApplication", "onStop ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("myApplication", "onDestroy ");
    }
}

//**Second_Activity**
public class Second_Activity  extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("second_activity", "onCreate ");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("second_activity", "onStart ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("second_activity", "onResume ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("second_activity", "onPause ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("second_activity", "onStop ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("second_activity", "onDestroy ");
    }
}

//**activity_main**
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第二個界面"
        android:id="@+id/button"
        android:layout_margin="@dimen/activity_horizontal_margin"
        />
</RelativeLayout>

//**layout_second**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</LinearLayout>

//**AndroidManifest**
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.myapplication" >

    <application
        android:icon="@mipmap/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=".Second_Activity"></activity>
    </application>
</manifest>

Button LinearLayout佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dip"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="match_parent" />
        <LinearLayout
            android:layout_width="0dip"
            android:layout_weight="2"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <Button
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_height="0dip" />
            <Button
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_height="0dip" />

        </LinearLayout>

    </LinearLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dip" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dip"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <Button
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_height="0dip" />

            <Button
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_height="0dip" />

            <Button
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_height="0dip" />

        </LinearLayout>
        <Button
            android:layout_width="0dip"
            android:layout_weight="2"
            android:layout_height="match_parent" />

    </LinearLayout>

</LinearLayout>
![這裏寫圖片描述](http://img.blog.csdn.net/20150818200123407)

Relative屬性

//**margin屬性**    不需要加ID
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:id="@+id/button3" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_alignParentBottom="true"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_alignParentRight="true"/>
</RelativeLayout>
![這裏寫圖片描述](http://img.blog.csdn.net/20150819194847046)
//**gravity屬性**   需要加ID
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="#ff0000"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/button"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/button"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/button"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/button"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/button"
        android:layout_above="@id/button"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/button"
        android:layout_below="@id/button"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/button"
        android:layout_alignTop="@id/button"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/button"
        android:layout_alignBottom="@id/button"/>


</RelativeLayout>
![這裏寫圖片描述](http://img.blog.csdn.net/20150818200556857)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章