2010-11-08 5 views
1

En essayant de placer une image et du texte dans ma vue, l'image étant deux fois plus grande que le texte, de sorte que les deux lignes de texte puissent être placées à côté de l'image, si:Problème lors de la mise en place d'un LinearLayout dans un TableLayout

_____ 
|  |text here 
|_____|text here 

la façon dont j'essaie de le faire est de mettre les deux TextViews dans un LinearLayout, puis placez le ImageView et le LinearLayout, contenant le texte, dans une TableLayout avec une ligne et deux colonnes.

Lorsque je fais cela, je ne vois que l'ImageView. En fait, même lorsque je commente l'ajout de ImageView à la table, le texte à l'intérieur de LinearLayout n'apparaîtra pas du tout.

Toute aide avec le code ou les différentes approches de la mise en page est grandement appréciée.

LinearLayout tempVindLayout = new LinearLayout(this); 
    tempVindLayout.setOrientation(LinearLayout.VERTICAL); 
    tempVindLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

    TableLayout tabellLayout = new TableLayout(this); 
    tabellLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
    tabellLayout.setPadding(0, 60, 0, 0); 

    TableRow row1 = new TableRow(this); 
    row1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

    // Textview saturday 
    TextView tempSat = new TextView(this); 
    tempSat.setText("+13"); 
    tempSat.setTextAppearance(this, R.style.RegularSmall); 
    tempSat.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

    // Wind saturday 
    TextView vindSat = new TextView(this); 
    vindSat.setText("5 m/s"); 
    vindSat.setTextAppearance(this, R.style.RegularSmall); 
    vindSat.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

    //Image for weather icons 
    ImageView imgSat = new ImageView(this); 
    imgSat.setImageResource(R.drawable.solmoln); 
    imgSat.setAdjustViewBounds(true); 

    // Add to table 
    tempVindLayout.addView(tempSat); 
    tempVindLayout.addView(vindSat); 

    row1.addView(tempVindLayout); 
    row1.addView(imgSat); 

    tabellLayout.addView(row1); 

    setContentView(tabellLayout); 
+0

Il est beaucoup plus facile de voir que se passe-t-il si vous utilisez un fichier xml pour créer la mise en page, au lieu de le faire en code? –

Répondre

0

La même chose peut être réalisé en utilisant la mise en page suivante ...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
<ImageView android:id="@+id/ImageView_01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/image" /> 
<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_toRightOf="@+id/ImageView_01"> 
    <TextView android:id="@+id/TextView_01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Header Text" 
     android:textColor="@android:color/white"/> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Header Text" 
     android:textColor="@android:color/white" 
     android:layout_below="@+id/TextView_01"/> 
    </RelativeLayout> 
</RelativeLayout> 
0

Peut-être que quelque chose comme cela serait utile:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher" /> 

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

</LinearLayout> 

Questions connexes