我试图以编程方式创建一些视图。这里看起来应该是什么样的:
!
这就是我所拥有的:
!
这是我的布局:
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:id="@+id/choose_activity_dialog_table_layout">
<TableRow>
<!-- ACTIVITY TYPE -->
<RelativeLayout
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="left">
<ImageView
android:layout_width="match_parent"
android:layout_height="40dp"
android:src="@drawable/pct_equitation_white"
android:scaleType="centerInside"
/>
<TextView
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test"
android:gravity="center"
android:textColor="@color/white"
/>
</RelativeLayout>
<!-- ACTIVITY TYPE -->
<RelativeLayout
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_gravity="left"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="40dp"
android:src="@drawable/pct_amhe_white"
android:layout_gravity="center_horizontal"
android:scaleType="centerInside"
/>
<TextView
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test"
android:gravity="center"
android:textColor="@color/white"
/>
</RelativeLayout>
<!-- ACTIVITY TYPE -->
<RelativeLayout
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_gravity="left"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="40dp"
android:src="@drawable/pct_ecriture_white"
android:layout_gravity="center_horizontal"
android:scaleType="centerInside"
/>
<TextView
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test"
android:gravity="center"
android:textColor="@color/white"
/>
</RelativeLayout>
</TableRow>
</TableLayout>
</LinearLayout>
这是我的循环:
View v = inflater.inflate(R.layout.choose_activity_dialog_view, container, false);
int count = 1;
TableRow row = new TableRow(getContext());
TableLayout mainLayout = (TableLayout) v.findViewById(R.id.choose_activity_dialog_table_layout);
for(UserActivity ua : HomeActivity.userActivityStore.getUserActivities()){
Activity currentActivity = HomeActivity.activityStore.findByActivityId(ua.getActivityId());
if(currentActivity == null)
continue;
if(count % 3 == 0){
count = 1;
mainLayout.addView(row);
row = new TableRow(v.getContext());
}
ImageView pictogramImageView = new ImageView(getContext());
final int pictogramImageViewWidth = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
RelativeLayout.LayoutParams pictogramImageViewParams = new RelativeLayout.LayoutParams(pictogramImageViewWidth, RelativeLayout.LayoutParams.MATCH_PARENT);
pictogramImageView.setLayoutParams(pictogramImageViewParams);
pictogramImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
pictogramImageView.setImageDrawable(getResources().getDrawable(R.drawable.pct_amhe));
TextView activityNameTextView = new TextView(getContext());
RelativeLayout.LayoutParams activityNameLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
activityNameLayoutParams.setMargins(
0,
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()),
0,
0
);
activityNameTextView.setLayoutParams(activityNameLayoutParams);
activityNameTextView.setGravity(Gravity.CENTER);
activityNameTextView.setTextColor(Color.WHITE);
activityNameTextView.setText(currentActivity.getName());
RelativeLayout rowRelativeLayout = new RelativeLayout(getContext());
final int rowRelativeLayoutWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
TableRow.LayoutParams params = new TableRow.LayoutParams(rowRelativeLayoutWidth, TableRow.LayoutParams.WRAP_CONTENT, 1f);
rowRelativeLayout.setGravity(Gravity.LEFT);
rowRelativeLayout.setLayoutParams(params);
rowRelativeLayout.addView(pictogramImageView);
rowRelativeLayout.addView(activityNameTextView);
row.addView(rowRelativeLayout, count - 1);
count++;
}
mainLayout.addView(row);
我不知道这里的问题是什么,我一直在寻找几个小时的主题和内容,而我却无法做到......
答案 0 :(得分:0)
我提出了一个布局,并且速度更快。
View v = inflater.inflate(R.layout.choose_activity_dialog_view, container, false);
int count = 1;
TableRow row = new TableRow(getContext());
TableLayout mainLayout = (TableLayout) v.findViewById(R.id.choose_activity_dialog_table_layout);
for(UserActivity ua : HomeActivity.userActivityStore.getUserActivities()){
Activity currentActivity = HomeActivity.activityStore.findByActivityId(ua.getActivityId());
if(currentActivity == null)
continue;
View chooseActivityView = inflater.inflate(R.layout.choose_activity_item, (ViewGroup) row, false);
RelativeLayout relativeLayout = (RelativeLayout) chooseActivityView.findViewWithTag("relative_layout");
row.addView(relativeLayout);
if(count % 3 == 0){
count = 0;
mainLayout.addView(row);
row = new TableRow(getContext());
}
count++;
我的布局如下:
<?xml version="1.0" encoding="utf-8"?>
<!-- ACTIVITY TYPE -->
<RelativeLayout
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:tag="relative_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_marginBottom="25dip">
<ImageView
android:layout_width="match_parent"
android:layout_height="40dp"
android:src="@drawable/pct_amhe_white"
android:layout_gravity="center_horizontal"
android:scaleType="centerInside"
/>
<TextView
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test"
android:gravity="center"
android:textColor="@color/white"
/>
</RelativeLayout>
我建议人们寻找解决他们问题的方法,膨胀的第二个参数会对渲染产生很大的影响。