轉載請註明出處: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的三種實現基本方式。