2010-11-08 85 views
1

我试图在图像中放置图像和一些文本,图像的高度是文本高度的两倍,以便两行文本可以放在图像的旁边,就像所以:在TableLayout中放置LinearLayout的问题

_____ 
|  |text here 
|_____|text here 

我试图做到这一点的方法是将两个TextViews在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); 
+0

这是很容易看到如果您使用xml文件创建布局,而不是在代码中执行,会发生什么情况。 – 2010-11-08 22:56:06

回答

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>