Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 中使用代碼動態布局

Android 中使用代碼動態布局

編輯:關於Android編程

Android 中使用代碼動態布局

 

本文介紹在android中使用代碼動態布局,有時候根據不同的需求,比如需要根據服務器上的條目個數來決定app中頁面布局控件(顯示個數,圖標等)。此處介紹通過java代碼進行動態布局。

 

一、效果圖:

\

圖片隨便找的,將就將就吧吐舌頭

 

二、給出xml文件布局

 

 

 

三、子條目xml布局文件

 

<framelayout android:clickable="true" android:layout_height="84dp" android:layout_weight="1.0" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android">

    

    

</framelayout>

如圖:\

 

 


 

四、java代碼動態布局

 

/**
 * @author gao_chun
 *
 */
public class MainActivity extends Activity implements OnClickListener{

    private ViewGroup mMoreLayout;  //父布局容器(動態加載的資源圖片和文字等布局都將添加在其裡面)

    /* (non-Javadoc)
     * @see app.ui.TitleActivity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initUI();   //保證啟動方法的唯一性
    }

    private void initUI() {
        setContentView(R.layout.activity_main);

        //找到該容器(這裡的控件為LinearLayout,轉換為ViewGroup是因為ViewGroup是容器的基類)
        mMoreLayout = (ViewGroup) findViewById(R.id.layout_more);

        //由於文字也是動態生成,使用android中array文件定義資源文件,並取出
        final String[] categories = getResources().getStringArray(R.array.categories);

        final int size = categories.length;     //String[]的長度
        final int rowCount = size / 3;          //需要布局的行數(每行三個)

        /**
         * 動態添加布局方法封裝
         * 參數 1.父容器    2.資源文字數組  3.從第幾個開始   4.行數
         */
        fillViews(mMoreLayout, categories, 0, rowCount);
    }

    private void fillViews(ViewGroup layout, String[] categories, int start, int end) {
        // 表格第一條線
        View.inflate(this, R.layout.layout_line_horizonal, layout);

        for (int i = start; i < end; i++) {

            //找到索引,便於根據索引添加圖片文件和文字
            final int firstIndex = i * 3;
            final int secondIndex = i * 3 + 1;
            final int thirdIndex = i * 3 + 2;

            final String firstCategory = categories[firstIndex];
            final String secondCategory = categories[secondIndex];
            final String thirdCategory = categories[thirdIndex];

            //這裡控制的是加載本地圖片,通過應用包命找到 有規則命名的圖片資源文件
            //--->因為這裡有兩種效果,一是默認的圖片,二是按下觸發後的圖片和文字
            final int firstDrawableNormal = getResources().getIdentifier(String.format(ic_department_%02d_normal,
                    firstIndex + 1),drawable,getApplicationContext().getPackageName());
            final int secondDrawableNormal = getResources().getIdentifier(String.format(ic_department_%02d_normal,
                    secondIndex + 1),drawable,getApplicationContext().getPackageName());
            final int thirdDrawableNormal = getResources().getIdentifier(String.format(ic_department_%02d_normal,
                    thirdIndex + 1),drawable,getApplicationContext().getPackageName());
            final int firstDrawablePressed = getResources().getIdentifier(String.format(ic_department_%02d_pressed,
                    firstIndex + 1),drawable,getApplicationContext().getPackageName());
            final int secondDrawablePressed = getResources().getIdentifier(String.format(ic_department_%02d_pressed,
                    secondIndex + 1),drawable,getApplicationContext().getPackageName());
            final int thirdDrawablePressed = getResources().getIdentifier(String.format(ic_department_%02d_pressed,
                    thirdIndex + 1),drawable,getApplicationContext().getPackageName());

            //這裡是將上面找到的   默認圖片  和  按下時的圖片 放入到  StateListDrawable緩存中
            final StateListDrawable firstDrawable = new StateListDrawable();
            firstDrawable.addState(new int[]{android.R.attr.state_pressed}, getResources().getDrawable(firstDrawablePressed));
            firstDrawable.addState(new int[]{}, getResources().getDrawable(firstDrawableNormal));

            final StateListDrawable secondDrawable = new StateListDrawable();
            secondDrawable.addState(new int[]{android.R.attr.state_pressed}, getResources().getDrawable(secondDrawablePressed));
            secondDrawable.addState(new int[]{}, getResources().getDrawable(secondDrawableNormal));

            final StateListDrawable thirdDrawable = new StateListDrawable();
            thirdDrawable.addState(new int[]{android.R.attr.state_pressed}, getResources().getDrawable(thirdDrawablePressed));
            thirdDrawable.addState(new int[]{}, getResources().getDrawable(thirdDrawableNormal));


            // 父布局
            final LinearLayout linearLayout = new LinearLayout(this);

            // 第一個子布局
            View.inflate(this, R.layout.layout_line_vertical, linearLayout);
            View.inflate(this, R.layout.layout_department, linearLayout);
            View.inflate(this, R.layout.layout_line_vertical, linearLayout);

            // 第二個子布局
            View.inflate(this, R.layout.layout_department, linearLayout);
            View.inflate(this, R.layout.layout_line_vertical, linearLayout);

            // 第三個子布局
            View.inflate(this, R.layout.layout_department, linearLayout);
            View.inflate(this, R.layout.layout_line_vertical, linearLayout);

            LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
            layout.addView(linearLayout, layoutParams);

            // 表格最後一條線
            View.inflate(this, R.layout.layout_line_horizonal, layout);

            //根據索引getChildAt到指定的位置
            final View firstView = linearLayout.getChildAt(1);
            firstView.setTag(firstCategory);        //設置tag,便於在後面判斷點擊的哪一個
            firstView.setOnClickListener(this);     //設置點擊
            final TextView firstTextView = (TextView) firstView.findViewById(R.id.text_title);
            firstTextView.setText(firstCategory);   //設置文字
            final ImageView firstImageView = (ImageView) firstView.findViewById(R.id.image_icon);
            firstImageView.setImageDrawable(firstDrawable); //將之前緩存的圖片設置出來

            final View secondView = linearLayout.getChildAt(3);
            secondView.setTag(secondCategory);
            secondView.setOnClickListener(this);
            final TextView secondTextView = (TextView) secondView.findViewById(R.id.text_title);
            secondTextView.setText(secondCategory);
            final ImageView secondImageView = (ImageView) secondView.findViewById(R.id.image_icon);
            secondImageView.setImageDrawable(secondDrawable);

            final View thirdView = linearLayout.getChildAt(5);
            thirdView.setTag(thirdCategory);
            thirdView.setOnClickListener(this);
            final TextView thirdTextView = (TextView) thirdView.findViewById(R.id.text_title);
            thirdTextView.setText(thirdCategory);
            final ImageView thirdImageView = (ImageView) thirdView.findViewById(R.id.image_icon);
            thirdImageView.setImageDrawable(thirdDrawable);

        }
    }

    /* (non-Javadoc)
     * @see app.ui.TitleActivity#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        final Object tag = v.getTag();      //通過之前setTag找到點擊位置
        if (tag != null) {
            String department = (String) tag;
            Toast.makeText(this, department, 0).show();
        } // else ignored
    }
}

在onClick事件中通過布局時設置的Tag找出用戶點擊的是哪一個具體的Layout

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved