標籤:android
簡介
相對布局的組件是由兄弟組件和父組價決定的,因此這種布局被稱為相對布局。
屬性設定介紹
RelativeLayout.Layoutparam中只能設定為true和false的屬性是:
需要依賴其他組件Id設定的屬性:
除此之外,RelativeLayout還可以使用viewGroup.MarginLayoutparams的屬性。
梅花布局的執行個體
<LinearLayout 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" android:orientation="vertical"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/ic_launcher" android:id="@+id/view01" android:layout_centerInParent="true"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/ic_launcher" android:layout_below="@id/view01" android:layout_alignLeft="@id/view01"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/ic_launcher" android:layout_above="@id/view01" android:layout_alignLeft="@id/view01"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/ic_launcher" android:layout_toLeftOf="@id/view01" android:layout_alignTop="@id/view01"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/ic_launcher" android:layout_toRightOf="@id/view01" android:layout_alignTop="@id/view01"/> </RelativeLayout></LinearLayout>
表格版面配置
GridLayout是4.0以後才加入的布局,把這個空間分成rows*columns個格子,一個組件可以設定橫跨多少列和多少行。
gridLayout的屬性
常用屬性:
GridLayout.Layoutparam的屬性:
計算機介面的執行個體
package peng.liu.testview;import android.annotation.TargetApi;import android.app.Activity;import android.os.Build;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.Gravity;import android.widget.Button;import android.widget.GridLayout;import android.widget.TextView;import java.util.Timer;import java.util.TimerTask;public class MainActivity extends Activity { GridLayout grid = null; String[] chars = new String[]{ "7","8","9","/", "4","5","6","*", "1","2","3","-", ".","0","+","+" }; @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); grid = (GridLayout) findViewById(R.id.root); for (int i=0;i<chars.length;i++){ Button button = new Button(this); button.setText(chars[i]); button.setTextSize(40); GridLayout.Spec rowSpec = GridLayout.spec(i / 4 + 2); GridLayout.Spec columnSpec = GridLayout.spec(i%4); GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpec,columnSpec); params.setGravity(Gravity.FILL); grid.addView(button,params); } }}
<LinearLayout 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" android:orientation="vertical"> <GridLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/root" android:rowCount="6" android:columnCount="4"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_columnSpan="4" android:layout_marginLeft="4px" android:layout_marginRight="4px" android:padding="5px" android:textSize="50sp" android:layout_gravity="right" android:background="#eee" android:textColor="#000" android:text="0" /> </GridLayout></LinearLayout>
絕對布局
因為絕對布局,需要親自設定組件的x位置,和y位置,所以一定程度上已經過時了。
屬性
android:layout_x:設定組件的z座標
android:layout_y:設定組件的Y座標
可以這麼說相對布局已經過時了~
距離單位介紹
android布局-android學習之旅(十六)