Android學習系列(二)布局管理器之線性布局的3種實現方式,android線性
轉載請註明出處: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 {@Overrideprotected 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裡面添加TextViewaddView(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個帶動畫的ImageViewaddView(iv, params);}}/** * 對子view進行布局,確定子view的位置 */@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {super.onLayout(changed, l, t, r, b);}/** * 測量尺寸時的回調方法 */@Overrideprotected 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:動畫執行時間3srepeatCount: 重複次數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的三種實現基本方式。
源碼
怎更改eclipse中的布局管理器的預設布局形式(android開發時想用線性布局 可預設是相對布局
右鍵layout,點new,選擇 android XML file ,預設就是Linearlayout
android,線性布局1,裡面嵌套線性布局2(垂直排列),想讓2在1裡是垂直置中的,怎搞?
這個問題我遇到過。
你想讓線性布局2在1裡置中,你就在布局1裡面緊貼一個線性布局3,然後讓布局2在布局3裡置中就可以了。給你寫個示意代碼:
<LinearLayout Android:id="布局1" layout_width=fill layout_height=fill>
<LinearLayout Android:id="布局3" layout_width=fill layout_height=fill gravity="center">
<LinearLayout id=布局2 width height ....... >
</LinearLayout><!-- End of 2 -->
</LinearLayout><!-- End of 3 -->
</LinearLayout><!-- End of 1 -->
問題解決。在布局3裡,該咋地就咋地。用margin也好,用gravity=center也好,都可以有效果了。