布局代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.wsd.sun.myapplication.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ebebeb"
android:orientation="vertical"
android:scrollbars="none">
<LinearLayout
android:id="@+id/get_lottery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="vertical" />
</ScrollView>
</LinearLayout>
代码
public class MainActivity extends AppCompatActivity { private LinearLayout mLayout; private Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context=this; mLayout= (LinearLayout) findViewById(R.id.get_lottery); initLottery(); } private void initLottery() { mLayout.removeAllViews(); final String[] tags = new String[]{"去测试0","去测试1","去测试2","去测试3","去测试4","去测试5","去测试6"}; if (tags != null && tags.length > 0) { mLayout.setVisibility(View.VISIBLE); for ( int i = 0; i < tags.length ; i++) { final String name_str = tags[i]; View view = LayoutInflater.from(context).inflate(R.layout.list_item_select_lottery, null); TextView nameView = (TextView) view.findViewById(R.id.list_item_lottery); LinearLayout layoutView = (LinearLayout) view.findViewById(R.id.list_item_layout); nameView.setText(name_str); nameView.setTextColor(0xff4d4d4d); layoutView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context,"点击了:"+name_str,Toast.LENGTH_SHORT).show(); } }); mLayout.addView(view); } } else { mLayout.setVisibility(View.GONE); } } }
demo地址:http://download.csdn.net/detail/wang1897b7/9714057