2016-04-15 11 views
0

プログラムでいくつかのビューを作成しようとしています。それは同じように見えるべきものです:Androidプログラムによって作成されたコンテンツが正しく表示されない

1

これは私が持っているものです。

2

これは私のレイアウトです:

  <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

目的のビューはxmlを使用して簡単にレイアウトできます。あなたがプログラムでそれをやりたがっている理由はありますか? – Elye

答えて

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> 

私はインフレートの第2引数はレンダリングについて多くのことを変更し、その問題の解決策を探している人にお勧めします。

関連する問題