2010-11-08 2 views
1

Im пытается поместить изображение и некоторый текст в мой взгляд, при этом изображение будет в два раза больше высоты текста, чтобы две строки текста могли быть размещены рядом с изображением, например так:Проблема с размещением LinearLayout внутри TableLayout

_____ 
|  |text here 
|_____|text here 

путь я пытаюсь сделать это, чтобы положить два TextViews в LinearLayout, затем поместите ImageView и LinearLayout, содержащий текст, в TableLayout с одной строки и двух столбцов.

Когда я делаю это, я вижу только изображение. На самом деле, даже когда я комментирую добавление ImageView в таблицу, текст внутри LinearLayout вообще не появляется.

Любая помощь с кодом или различными подходами к макету очень ценится.

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

Это намного проще, чтобы увидеть что происходит, если вы используете xml-файл для создания макета вместо того, чтобы делать это в коде. –

ответ

0

Подобная вещь может быть достигнуто с помощью следующей раскладку ...

<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

Может быть что-то подобное было бы полезно:

<?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> 

Смежные вопросы