Android學習系列(二)布局管理器之線性布局的3種實現方式

來源:互聯網
上載者:User

標籤:示範   時間   spec   launch   分享   程式   圖片   檔案   字型   

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


原始碼






Android學習系列(二)布局管理器之線性布局的3種實現方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.