Textview中的文本扩展到屏幕宽度之外

时间:2011-08-17 13:22:12

标签: android user-interface layout textview

我在函数createPerennialItem()中动态创建表行。问题是textview(例如id = tvRowNameStringDynamicItem的那个)如果文本不适合则扩展到屏幕宽度之外。该问题仅发生在2.3.1之前的Android上。

我尝试使用setSingleLine()以及android:inputType="textMultiLine"但没有运气。

任何有关如何解决它的帮助表示赞赏。以下是相关的代码段:

private void createPerennialItem(int itemIndex) {

    PerennialItem perennialItem = mCurItems.get(itemIndex);


    LayoutInflater inflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    TableRow rowView = (TableRow) inflater.inflate(
            R.layout.table_row_element_layout, null);
    TableLayout table = (TableLayout) findViewById(R.id.PlantItemsTableLayout);

    // create a new TableRow
    TableRow row = new TableRow(this);
    row.addView(rowView.findViewById(R.id.ListPlantItemsTableRow));

    TextView tvPlantName = (TextView) rowView.findViewById(R.id.tvRowNameStringDynamicItem);
    tvPlantName.setText(Util.Capitilize(perennialItem.getPlantName()));


    ImageView ivPlantImage = (ImageView) rowView.findViewById(R.id.ivRowPerennialIconDynamicItem);
    try {
        String imagePath = Perenner.thumbPath + perennialItem.getPhotoName();
        InputStream stream = getAssets().open(imagePath);
        Bitmap tmpBmp = BitmapFactory.decodeStream(stream, null, null);
        ivPlantImage.setImageBitmap(tmpBmp);

        if (stream != null)
            stream.close();

    } catch (final IOException ex) {
        Util.debugPrint(ex.getMessage(), Util.LOG_E);
    }

    TextView tmpView = (TextView) rowView.findViewById(R.id.tvRowPlantColor);
    tmpView.setText(getString(R.string.tv_show_item_plant_color) 
                    + Util.Capitilize(perennialItem.getPlantColor()));

    tmpView = (TextView) rowView.findViewById(R.id.tvRowPlantHabitat);
    tmpView.setText(getString(R.string.tv_show_item_plant_habitat) 
                    + Util.Capitilize(perennialItem.getPlantHabitat()));

    tmpView = (TextView) rowView.findViewById(R.id.tvRowPlantFTime);
    tmpView.setText(getString(R.string.tv_show_item_plant_ftime) 
                    + Util.Capitilize(perennialItem.getPlantFTime()));

    tmpView = (TextView) rowView.findViewById(R.id.tvRowPlantHeight);
    tmpView.setText(getString(R.string.tv_show_item_plant_height) 
                    + Util.Capitilize(perennialItem.getPlantHeight()));

    tmpView = (TextView) rowView.findViewById(R.id.tvRowPlantSoil);
    tmpView.setText(getString(R.string.tv_show_item_plant_soil) 
                    + Util.Capitilize(perennialItem.getPlantSoil()));

    if (mCurItemPhotos.get(itemIndex).getFileName().equalsIgnoreCase("missing.png")) {
        row.setClickable(false);
        TextView tvGT = (TextView) rowView.findViewById(R.id.tvRowGTStringDynamicItem);
        tvGT.setVisibility(View.INVISIBLE);
        row.setId(btnDummyIds[itemIndex]);
    } else {
        row.setClickable(true);
        row.setFocusable(true);
        row.setId(btnDummyIds[itemIndex]);
        TextView tvGT = (TextView) rowView.findViewById(R.id.tvRowGTStringDynamicItem);
        tvGT.setVisibility(View.VISIBLE);
        row.setOnClickListener(this);
    }

    TableLayout.LayoutParams p = new TableLayout.LayoutParams(
            TableLayout.LayoutParams.WRAP_CONTENT,
            TableLayout.LayoutParams.WRAP_CONTENT);
    p.setMargins(0, 0, 0, 5);

    // add the TableRow to the TableLayout
    table.addView(row, p);
}

每个动态创建的行都会添加到下面的TableLayout中:

<TableLayout android:id="@+id/PlantItemsTableLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_below="@+id/tvPlants"
    android:stretchColumns="*"
    android:shrinkColumns="*"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp">            
</TableLayout>

每行都构建为膨胀以下xml代码:

<TableRow xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ListPlantItemsTableRow" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    style="@style/Dynamic_Button_Layout"

    >
    <!-- Layout used in ShowItemActivity to create dynamic rows for Plants -->
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/DynamicRowLayout" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"

    >

      <TextView
        android:id="@+id/tvRowNameStringDynamicItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="@dimen/header3_font_size"
        android:textStyle="bold"
        android:text="Name"
        android:textColor="@color/blackColor"
        android:paddingTop="5dp"
        android:paddingLeft="5dp"
        android:layout_gravity="left|center_vertical"
        android:inputType="textMultiLine"
      />
      <ImageView
        android:id="@+id/ivRowPerennialIconDynamicItem"
        android:layout_width="80dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="5dp"
        android:layout_height="92dp"
        android:src="@drawable/missing"
        android:layout_below="@+id/tvRowNameStringDynamicItem"
      />
      <!-- Content to the right of image -->
      <TextView
        android:id="@+id/tvRowPlantColor"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/listview_row_font_size"
        android:textColor="@color/blackColor"
        android:text="@string/tv_show_item_plant_color"
        android:layout_below="@+id/tvRowNameStringDynamicItem"
        android:layout_toRightOf="@+id/ivRowPerennialIconDynamicItem"
        android:layout_marginRight="22dp"
      />
      <TextView
        android:id="@+id/tvRowPlantHabitat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/listview_row_font_size"
        android:textColor="@color/blackColor"
        android:text="@string/tv_show_item_plant_habitat"
        android:layout_below="@+id/tvRowPlantColor"
        android:layout_toRightOf="@+id/ivRowPerennialIconDynamicItem"
        android:layout_marginRight="22dp"
      />
      <TextView
        android:id="@+id/tvRowPlantFTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/listview_row_font_size"
        android:textColor="@color/blackColor"
        android:text="@string/tv_show_item_plant_ftime"
        android:layout_below="@+id/tvRowPlantHabitat"
        android:layout_toRightOf="@+id/ivRowPerennialIconDynamicItem"
        android:layout_marginRight="22dp"
      />
       <TextView
        android:id="@+id/tvRowPlantHeight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/listview_row_font_size"
        android:textColor="@color/blackColor"
        android:text="@string/tv_show_item_plant_height"
        android:layout_below="@+id/tvRowPlantFTime"
        android:layout_toRightOf="@+id/ivRowPerennialIconDynamicItem"
        android:layout_marginRight="22dp"
      />
       <TextView
        android:id="@+id/tvRowPlantSoil"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/listview_row_font_size"
        android:textColor="@color/blackColor"
        android:text="@string/tv_show_item_plant_soil"
        android:layout_below="@+id/tvRowPlantHeight"
        android:layout_toRightOf="@+id/ivRowPerennialIconDynamicItem"
        android:layout_marginRight="22dp"
      />

      <TextView
        android:id="@+id/tvRowGTStringDynamicItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/header3_font_size"
        android:textColor="@color/blackColor"
        android:textStyle="bold"
        android:text=">" 
        android:layout_gravity="center_vertical"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:layout_centerInParent="true"

      />
    </RelativeLayout>
</TableRow>

0 个答案:

没有答案