【问题标题】:android customize tablerow with imageview overflow tablerowandroid用imageview溢出tablerow自定义tablerow
【发布时间】:2015-09-22 05:21:34
【问题描述】:

我正在尝试创建一个表格布局来显示一个用户的个人资料图片和他的姓名。除了布局是有线的外,一切正常。图像视图溢出表格行。我设置了 tablerow layout_height="70dp" 和 iamgeview layout_height ="50dp"layout_margin="10dp" 以便垂直集中图像。 表格行 xml:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="70dp"
    android:orientation="horizontal"
    android:layout_marginBottom="1dp"
    android:background="#fff">
    <ImageView
        android:layout_height="50dp"
        android:layout_width="50dp"
        android:id="@+id/pic"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:background="#f00"/>
    <TextView
        android:layout_height="20dp"
        android:layout_width="wrap_content"
        android:layout_marginTop="25dp"
        android:layout_marginRight="25dp"
        android:id="@+id/value"
        android:textColor="#000"
        android:layout_alignParentRight="true"
        android:layout_weight="3.0"
        android:textAlignment="gravity"
        android:gravity="right"
        />
</TableRow>

表格布局xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fff">
        <TableLayout android:id="@+id/contact_table"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#aaa">
        </TableLayout>
</ScrollView>

java代码:

table =(TableLayout)findViewById(R.id.contact_table);
                    LayoutInflater inflater = getLayoutInflater();
                    TableRow row = (TableRow)inflater.inflate(R.layout.profilerow, table, false);
                    ImageView tag = (ImageView)row.findViewById(R.id.pic);
                    new DownloadImageTask(tag).execute(me.getDp_address());
                    TextView value = (TextView)row.findViewById(R.id.value);
                    value.setText(me.getFirst_name()+" "+me.getLast_name());
                    table.addView(row);

                    TableRow row2 = (TableRow)inflater.inflate(R.layout.centertextrow,table ,false);
                    table.addView(row2);



private class DownloadImageTask extends AsyncTask<String,Void,Bitmap>{
        ImageView img;
        public DownloadImageTask(ImageView bmImage){
            this.img = bmImage;
        }
        @Override
        protected Bitmap doInBackground(String... urls) {
            String url_string = urls[0];
            Bitmap micoin = null;
            try {
                InputStream in = new java.net.URL(url_string).openStream();
                micoin = BitmapFactory.decodeStream(in);
            } catch (IOException e) {
                Log.i("michaellog","error a");
                //Log.e("michaellog",e.getMessage());
                e.printStackTrace();
            }
            return micoin;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            img.setImageBitmap(bitmap);
        }
    }

这是它的外观图片:link

图像向下移动。

【问题讨论】:

    标签: android imageview tablelayout tablerow


    【解决方案1】:

    你好!

    在为我们的大学应用程序创建一个放入表格视图的 youtube 提要列表时,我遇到了相同类型的问题。我从我的布局和你的布局中看到的唯一区别是我在创建的 tableview 单元格中添加了一个 android:scaleType="centerCrop" 。这应该缩放您的图像以适合正确的区域。对于表格中要填充的每个单元格,我还使用线性布局而不是 tableRow 类型。我也会在这里查看答案。 https://stackoverflow.com/a/15832564/877568

     <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:id="@+id/semesterButtonLL"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <ImageView android:layout_width="50dip" 
            android:id="@+id/image" 
            android:src="@drawable/icon72" 
            android:layout_height="50dip" 
            android:scaleType="centerCrop">
        </ImageView>
        <TextView android:text="TextView"
            android:id="@+id/text" 
            android:textColor="@color/usu_blue"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_gravity="left|center_vertical"
            android:textSize="20dip"
            android:layout_marginLeft="10dip">
        </TextView>
    </LinearLayout>
    

    至于您提供的代码,关于您如何引入数据以及如果它在您提供的链接上显示图像,那么它可能就像将 centerCrop 添加到您的 xml 中的行一样简单。我希望这会有所帮助。

    【讨论】:

    • 嘿,谢谢你的建议。我打开了链接。我意识到他们使用 ListView 而不是 tablelayout 和 scrollview。你用了什么?表格布局还是列表视图?
    • 列表视图。我个人将上面的代码用于单元格,然后父容器是一个相对布局,其中 listview 作为子级。与示例中的建议相同。这种创建表的方法是较旧的方法。 TableLayout 在它自己的布局中基本上做同样的事情,但我知道 listview 方法效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多