使用ImageView和TextView设计创建Android动态表,并在父权限处设置日期

时间:2016-01-17 10:05:27

标签: android dynamic alignment

I am not able to put two textview's to the right of ImageView and aligning the date to parent right is also getting difficult for me in dynamic creation. someone who have knowledge on this please help me. Design should be DYNAMIC.

    tl = (TableLayout) view.findViewById(R.id.mainLayout);

       TableRow row2 = new TableRow(getContext());
                    row2.setLayoutParams(new TableRow.LayoutParams(200, 230, 280));


                    TableLayout posterInfoTable = new TableLayout(getContext());
                    posterInfoTable.setBackgroundResource(R.drawable.whitegrey);
                    posterInfoTable.setLayoutParams(new TableLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 250));

                    TableRow posterNameRow = new TableRow(getContext());
                    TableLayout.LayoutParams lp = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
                    lp.setMargins(0, 0, 30, 0);
                    posterNameRow.setHorizontalGravity(Gravity.LEFT);
                    posterNameRow.setLayoutParams(lp);

                    ImageView posterImage = new ImageView(getContext());
                    posterImage.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
                    posterImage.setMaxHeight(100);
                    posterImage.setMaxWidth(100);
                    Picasso.with(getContext())

                            .load(R.drawable.dummyimage2)
                            .placeholder(R.drawable.dummyimage2)
                            .error(R.drawable.dummyimage2)
                            .transform(new RoundedTransformation(50, 4))
                            .resizeDimen(R.dimen.list_detail_image_size, R.dimen.list_detail_image_size)
                            .centerCrop()
                            .into(posterImage);
                    posterNameRow.addView(posterImage);

                    final TableRow nameRow = new TableRow(getContext());
                    nameRow.setPadding(40, 0, 0, 0);
                    nameRow.setLayoutParams(new TableLayout.LayoutParams(250, 100));
                    nameRow.setTag(db_id);

                    TextView posterName = new TextView(getContext());
                    posterName.setText(postman);
                    posterName.setTextColor(Color.BLACK);
                    posterName.setTextSize(18);
                    posterName.setPadding(10, 0, 0, 0);
                    posterName.setWidth(240);
                    // posterName.text
                    posterNameRow.addView(posterName);

                    TextView dateview = new TextView(getContext());
                    dateview.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT))
                    dateview.setText(date);
                    dateview.setPadding(50, 0, 0, 5);
                    dateview.setTextColor(Color.DKGRAY);
                    dateview.setTextSize(10);
                    dateview.setWidth(300);
                    dateview.setGravity(Gravity.RIGHT);
                    nameRow.addView(dateview);

                    posterInfoTable.addView(posterNameRow);
                    posterInfoTable.addView(nameRow);

                    //  posterInfoTable.addView(dateRow);

                    row2.addView(posterInfoTable, new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 200));
                    //   row2.addView(dateview);


                    tl.addView(row2, new TableLayout.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 200));

1 个答案:

答案 0 :(得分:0)

您无需动态创建所有视图。使用项目布局创建XML。试试这个:

void addNewRow(YourData data) { 
    View v = getLayoutInflater().inflate(R.layout.your_item, null);
    RelativeLayout rl = (RelativeLayout) v; //or other...
    TextView textView1 = (TextView) rl.findViewById(R.id.textV1);
    ImageView imageView1 = (ImageView) rl.findViewById(R.id.imageV1);
    textView1.setText(data.name);
    imageView.setImageResource(data.imageRes);
    tableLayout.addView(rl);
}