今天是我堅持寫博客的第三天,希望自己堅持下去,今天我要重溫的是佈局的嵌套。
大家還記得我們最常用的那幾種佈局麼,下面我們在回憶一下吧!
一、常用的五種佈局
(1)LinearLayout 線性佈局
線性佈局常用的屬性:android:orientation=""
橫向排列:horizontal
縱向排列:vertical
還有常用的權重weight
如果LinearLayout是最外面的一層,它是不會彈出layout_weight屬性的換句話說最外層不能用layout_weight
(2)RelativeLayout 相對佈局
相對佈局的屬性比較多;
相對一個控件的位置屬性;
android:layout_toLeftOf
android:layout_toRightOf
android:layout_above
android:layout_below
android:layout_alignBaseline
android:layout_alignLeft
android:layout_alignRight
android:layout_alignTop
android:layout_alignBottom
android:layout_alignParentLeft
android:layout_alignParentRight
android:layout_alignParentTop
android:layout_alignParentBottom
android:layout_centerInParent
android:layout_centerHorizontal
android:layout_centerVertical
android:layout_margin
android:layout_marginLeft
android:layout_marginRight
android:layout_marginTop
android:layout_marginBottom
基本屬性的應用,基本android studio和eclipsede編寫程序的時候可以自動補全和提示。
(3)TableLayout 表格佈局
(4)AbsoluteLayout 絕對佈局
後兩個佈局用的比較少,我以前寫過一個博客是關於常用的五種佈局,鏈接在下面:大家可以參考。
http://blog.csdn.net/qq_15776931/article/details/53494076
二、佈局的嵌套
我用Linearlayout實現嵌套的簡單代碼。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_qiaotaoactovity"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.zes.base.mydalytext.activity.QiantaoActovity"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="--請輸入您的賬號和密碼--"
android:textSize="20dp"
android:gravity="center"
android:layout_marginTop="15dp"
/>
<EditText
android:id="@+id/username_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入賬號:"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="15dp"
/>
<EditText
android:id="@+id/password_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入密碼:"
android:inputType="numberPassword"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="15dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="15dp">
<Button
android:id="@+id/login_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登錄"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"/>
<Button
android:id="@+id/zhuce_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="註冊"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
</LinearLayout>
</LinearLayout>
只是簡單的佈局嵌套實現。
三、針對LinearLayout的oritation屬性實現個簡單的代碼下面上截圖,實現的功能是點擊縱向按鈕佈局顯示爲縱向,點擊橫向按鈕實現橫向佈局。截圖:代碼:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import com.zes.base.mydalytext.R;
public class LinearlayoutActivity extends Activity implements View.OnClickListener{
private Button v_btn,h_btn;
private LinearLayout linearlayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_linearlayout);
v_btn = (Button) findViewById(R.id.v_btn);
h_btn = (Button) findViewById(R.id.h_btn);
linearlayout = (LinearLayout) findViewById(R.id.activity_linearlayout);
v_btn.setOnClickListener(this);
h_btn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.v_btn :
linearlayout.setOrientation(LinearLayout.VERTICAL);
break ;
case R.id.h_btn :
linearlayout.setOrientation(LinearLayout.HORIZONTAL);
break ;
}
}
}
今天寫的有點雜,希望不會影響大家閱讀!
——縱向——
——橫向——