在LinearLayout中實現列表,列表採用LinearLayout橫向佈局-android學習

很少講直接上代碼android

1.Activity 對應的佈局文件以下:app

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:fadingEdge="vertical">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:paddingTop="10dp"
        android:id="@+id/funtable">
        
    </LinearLayout>
    
</ScrollView>

2.Activity 實現類佈局

package com.test.appdemo1.actlearn;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import com.test.appdemo1.R;
import com.test.appdemo1.R.string;

/**
 * 功能學習 
 * @package com.test.appdemo1.actlearn
 * @author Administrator
 * @date 2014-3-1 下午11:16:31
 */
public class FunPageActivity extends Activity {

    private LinearLayout funListView = null;
    
    private Builder dialog = null;
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.funtest);
        funListView = (LinearLayout)findViewById(R.id.funtable);
        dialog = new Builder(this);
        initGridView();
    }

    /**
     *初使化UI 
     */
    private void initGridView() {
        int ii = 1008601;int idx = 1;
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,1f);                
        for(int i=1;i<=9;i++){
            LinearLayout layout = new LinearLayout(this);
            layout.setOrientation(LinearLayout.HORIZONTAL);
            for(int j=1;j<=3;j++){                            
                Button btn = new Button(this);
                String key = "funPageBtn"+idx;idx++;
                btn.setText(getString(getStringVal(key)));
                Log.d("FUNCTION",key);
                btn.setId(ii++);
                btn.setOnClickListener(new BtnClick());
                layout.addView(btn,param);
            }
            funListView.addView(layout);
        }
        
    }
    
    /**
     * 按紐單擊處理
     */
    class BtnClick implements OnClickListener{
        public void onClick(View v) {
            Button btn = (Button)v;
            //簡單顯示會話框
            showDialog(""+btn.getText(),btn.getText().toString()+"=="+btn.getId());
        }        
    }
    
    
    /**
     * 經過 R.string 類的屬性名獲取屬性值
     * @param str
     * @return
     */
    public int getStringVal(String str){
        try{
            Class<string> cls = R.string.class;
            return cls.getDeclaredField(str).getInt(null);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return 0;
    }
    
    
    /**
     * 會話框
     * @param title
     * @param msg
     */
    private void showDialog(String title,String msg){
        dialog.setIcon(R.drawable.ic_launcher).setPositiveButton("肯定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                arg0.cancel();
            }
        }).setTitle(title).setMessage(msg).show();
    }    
}

3.資源文件部分以下:學習

 <string name="funPageBtn1">底部選項卡</string>
    <string name="funPageBtn2">Fun 2</string>
    <string name="funPageBtn3">Fun 3</string>
    <string name="funPageBtn4">Dialog Test</string>
    <string name="funPageBtn5">Fun 2</string>
    <string name="funPageBtn6">Fun 3</string>
    <string name="funPageBtn7">Dialog Test</string>
    <string name="funPageBtn8">Fun 2</string>
    <string name="funPageBtn9">Fun 3</string>
    <string name="funPageBtn10">Dialog Test</string>
    <string name="funPageBtn11">Fun 2</string>
    <string name="funPageBtn12">Fun 3</string>
    <string name="funPageBtn13">Dialog Test</string>
    <string name="funPageBtn14">Fun 2</string>
    <string name="funPageBtn15">Fun 3</string>
    <string name="funPageBtn16">Dialog Test</string>
    <string name="funPageBtn17">Fun 2</string>
    <string name="funPageBtn18">Fun 3</string>
    <string name="funPageBtn19">Dialog Test</string>
    <string name="funPageBtn20">Fun 2</string>
    <string name="funPageBtn21">Fun 3</string>
    <string name="funPageBtn22">Dialog Test</string>
    <string name="funPageBtn23">Fun 2</string>
    <string name="funPageBtn24">Fun 3</string>
    <string name="funPageBtn25">Dialog Test</string>
    <string name="funPageBtn26">Fun 2</string>
    <string name="funPageBtn27">Fun 3</string>

4.效果ui


共同窗習進步!this

相關文章
相關標籤/搜索