将LinearLayout放在TableLayout中的问题

时间:2010-11-08 22:45:10

标签: android android-layout

我试图在我的视图中放置一个图像和一些文本,图像是文本高度的两倍,这样两行文本可以放在图像旁边,如下所示:

 _____
|     |text here
|_____|text here

我尝试这样做的方法是将两个TextView放在LinearLayout中,然后将包含文本的ImageView和LinearLayout放在一个包含一行和两列的TableLayout中。

当我这样做时,我只看到ImageView。事实上,即使我注释掉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);

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>