Android imageview 未显示在 TableRow 中

问题描述 投票:0回答:1

我试图在 TableRow 中显示一个简单的 ImageView,但由于某种原因它不会显示。如果我将另一个控件添加到另一行,imageView 会显示,所以看起来它只是没有强制正确的大小。我的xml如下:

<TableLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_height="fill_parent" 
  android:layout_width="fill_parent"
 android:background="#4B088A"
  android:id="@+id/ImageTable">

 <TableRow android:layout_width="fill_parent" 
 android:layout_height="wrap_content"
 android:padding="5dp"
 android:id="@+id/ImageTableRow"/> 

  </TableLayout>

我用来添加 ImageView 的代码是:

TableLayout tableLayout = (TableLayout) findViewById(R.id.ImageTable);  

TableRow tr = new TableRow(this);

TableRow.LayoutParams lp = new 
        TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT ,TableRow.LayoutParams.WRAP_CONTENT);

tr.setLayoutParams(lp);

 m_imageView = new MyImageView(getApplicationContext());

 m_imageView.setBackgroundColor(Color.GRAY);

 m_imageView.setImageBitmap(charty);

 tr.addView(m_imageView);

 tableLayout.addView(tr);
android imageview tablerow
1个回答
4
投票

试试这个代码:

将您的

activity_main.xml
写为

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <ImageView
                android:id="@+id/image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_action_search" />
        </TableRow>
    </TableLayout>
</LinearLayout>

ic_action_search
是“Drawable”文件夹中的图像,并将
MainActivity.java
写为

public class MainActivity extends Activity {
    
    ImageView image;
    
    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
} 
© www.soinside.com 2019 - 2024. All rights reserved.