Android imageview 未在 TableRow 中顯示 (Android imageview not displaying in TableRow)


問題描述

Android imageview 未在 TableRow 中顯示 (Android imageview not displaying in TableRow)

I am trying to display a simple ImageView in a TableRow but for some reason it won't display. If I add another control to another row the imageView does display, so it seems like it is just not forcing the size correctly. My xml is as follows:

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

The code that I am using to add the ImageView is:

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);

‑‑‑‑‑

參考解法

方法 1:

try this code

write your xml "activity_main" as

<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 is your image in drawable folder and write main activity as

public class MainActivity extends Activity 
{

ImageView image;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 }
} 

(by user1145533Sumedh Tambat)

參考文件

  1. Android imageview not displaying in TableRow (CC BY‑SA 3.0/4.0)

#imageview #tablerow #Android






相關問題

Android imageview 未在 TableRow 中顯示 (Android imageview not displaying in TableRow)

Android中的手勢 (Gestures in Android)

GridView kéo dài ImageView không đồng nhất (GridView Stretching ImageView Non-Uniformly)

ios在表格視圖單元格中視覺調整圖像大小 (ios visually resize image in a table view cell)

IndoorAtlas SDK 2.0:使用 Picasso 和自定義 ImageView (IndoorAtlas SDK 2.0: Using Picasso with custom ImageView)

當圖像的寬度小於顯示器的寬度時,如何在視圖尋呼機內對齊圖像視圖的中心? (How to align center an image view inside a view pager when the image's width is less than the display's?)

正確旋轉 ImageView 和 FrameLayout (Rotate ImageView and FrameLayout correctly)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

Android:擴展 onTouchEvent 以進行拖動的 ImageView (Android: ImageView that extends onTouchEvent for Drag)

將旋轉的文本放在圖像視圖上 (Put rotated text on an imageview)

如何在底部設置圖像視圖 (how to set imageview in bottom)

我如何使用數組來顯示匹配的 Imageview? (How Could I use array to show match Imageview?)







留言討論