Android學習系列(二)佈局管理器之線性佈局的3種實現方式
轉載請註明出處:http://blog.csdn.net/lhy_ycu/article/details/39643669
LinearLayout是Android控制元件中的線性佈局控制元件,它包含的子控制元件將以橫向(HORIZONTAL)或豎向(VERTICAL)的方式排列,按照相對位置來排列所有的子控制元件及引用的佈局容器。超過邊界時,某些控制元件將缺失或消失。因此一個垂直列表的每一行只會有一個控制元件或者是引用的佈局容器。
一、LinearLayout線性佈局的相關屬性說明:
android:orientation 佈局方向:"vertical"垂直線性佈局,"horizontal"水平線性佈局
android:id 為控制元件指定相應的ID
android:text 指定控制元件當中顯示的文字,需要注意的是,這裡儘量使用strings.xml檔案當中的字元
android:grivity 指定控制元件的基本位置,比如說居中,居右等位置
android:textSize 指定控制元件當中字型的大小
android:background 指定該控制元件所使用的背景色,RGB命名法
android:width 指定控制元件的寬度
android:height 指定控制元件的高度
android:padding 指定控制元件的內邊距,也就是說控制元件當中的內容
android:singleLine 如果設定為真的話,則將控制元件的內容在同一行當中進行顯示
android:layout_weight 預設值為0,layout_weight屬性可以控制各個控制元件在佈局中的相對大小,線性佈局會根據該控制元件layout_weight值與其· 所處佈局中所有控制元件layout_weight值之和的比值為該控制元件分配佔用的區域。
二、LinearLayout專案演示3種實現方式示例
2.1 第一種實現方式:xml配置實現LinearLayout
<activity_main.xml><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:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello!" android:textSize="20sp" /> </LinearLayout>
2.2 第二種實現方式:程式碼實現LinearLayout
<MainActivity.java>
</pre><pre name="code" class="java">/** * @author liu * @description 程式碼動態建立線性佈局管理器 */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); LinearLayout layout = new LinearLayout(this);// 建立現行佈局管理器 LinearLayout.LayoutParams params = new LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);// 設定線性佈局引數 layout.setOrientation(LinearLayout.VERTICAL); TextView txt = new TextView(this); LinearLayout.LayoutParams txtParams = new LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);// 設定元件引數 txt.setLayoutParams(txtParams); txt.setText("Hello!"); txt.setTextSize(20); layout.addView(txt, txtParams); addContentView(layout, params); } }
2.3 第三種實現方式:自定義實現LinearLayout(繼承LinearLayout)
2.3.1、實現效果圖(圖片旋轉)
2.3.2、專案結構圖
2.3.3、詳細的編碼實現
1)繼承LinearLayout的子類檔案MyLinearLayout.java:
public class MyLinearLayout extends LinearLayout {
/**
* 在xml佈局檔案中聲名的view,建立時由系統自動呼叫。
*/
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
/**
* 初始化LinearLayout檢視
*/
private void initView() {
// 設定LinearLayout的佈局方向
setOrientation(LinearLayout.VERTICAL);
// 設定佈局引數
LinearLayout.LayoutParams params = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(getContext());
tv.setText(R.string.hello_world);
// 在MyLinearLayout裡面新增TextView
addView(tv, params);
for (int i = 0; i < 10; i++) {
ImageView iv = new ImageView(getContext());
iv.setImageResource(R.drawable.ic_launcher);
Animation animation1 = AnimationUtils.loadAnimation(getContext(),
R.anim.rotate);
iv.setAnimation(animation1);
// 在MyLinearLayout裡面新增10個帶動畫的ImageView
addView(iv, params);
}
}
/**
* 對子view進行佈局,確定子view的位置
*/
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
}
/**
* 測量尺寸時的回撥方法
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
2)主佈局資原始檔,activity_main.xml:
<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:orientation="vertical" >
<com.liu.mylinearlayout01.MyLinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
3)動畫檔案rotate.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:fillBefore="false" >
<!--
旋轉效果,pivotX,pivotY指旋轉座標;
fillAfter="true" fillBefore="false" 表示動畫停止在最後的位置;
fromDegrees toDegrees從0°到350°
startOffset:延時1s執行
duration:動畫執行時間3s
repeatCount: 重複次數3+1
-->
<rotate
android:duration="3000"
android:fromDegrees="0"
android:pivotX="50%p"
android:pivotY="20%p"
android:repeatCount="3"
android:startOffset="1000"
android:toDegrees="350" />
</set>
4)、主Activity程式入口類,MainActivity.java:無需修改(按Eclipse自動生成的程式碼即可)
以上就是筆者知道的LinearLayout的三種實現基本方式。