不多讲直接上代码
1.Activity 对应的布局文件如下:
<?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.效果
共同学习进步!
在LinearLayout中实现列表,列表采用LinearLayout横向布局-android学习,布布扣,bubuko.com