创建动态表格布局时出现问题

发布于 2024-11-19 07:16:47 字数 2025 浏览 4 评论 0原文

我创建了一个动态 TableLayout,并为 TableRow 的背景提供了一个图像。但我的问题是图像采用其实际大小而不是我设置的大小。我希望该图像应采用我定义的高度和宽度。 请帮我。

我的代码是...

void createRows()
{
    for(int i=0;i<5;i++)
    {
        String name = "Hareesh Kumar Gangadhara";
        String time = "2:53 PM";
        String date = "06/05/11";
        String time_date =time+" "+date;
        String message = "Hello Prashant";
        TableRow row_message = new TableRow(this);
        ImageView friend_img = new ImageView(this);
        LinearLayout layout_msg_info = new LinearLayout(this);
        LinearLayout layout_name_date = new LinearLayout(this);
        LinearLayout layout_message = new LinearLayout(this);

        friend_img.setLayoutParams(new LayoutParams(35,35));

        friend_img.setBackgroundResource(R.drawable.archit);

        TextView tv_name = new TextView(this);
        TextView tv_time_date = new TextView(this);
        TextView tv_message = new TextView(this);

        tv_name.setText(name);
        tv_time_date.setText(time_date);
        tv_message.setText(message);

        tv_name.setTextColor(color_black);
        tv_time_date.setTextColor(color_black);
        tv_message.setTextColor(color_black);


        tv_name.setTextSize(14);
        tv_time_date.setTextSize(10);
        tv_message.setTextSize(12);


        layout_name_date.addView(tv_name);
        layout_name_date.addView(tv_time_date);
        layout_message.addView(tv_message);

        layout_msg_info.setOrientation(LinearLayout.VERTICAL);

        layout_msg_info.setPadding(30, 10, 0, 0);
        layout_msg_info.addView(layout_name_date);
        layout_msg_info.addView(layout_message);

        row_message.setBackgroundResource(R.drawable.img_message_back);
        row_message.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT,40));
        row_message.setPadding(0, 0, 10, 5);
        row_message.addView(friend_img);
        row_message.addView(layout_msg_info);

        table_message.addView(row_message);

    }
}

I have created a dynamic TableLayout and I have given an image to TableRow's Background.But my problem is the image is taking its actual size not what I am setting.I want that image should take the height and width defined by me.
Please Help Me.

My Code is...

void createRows()
{
    for(int i=0;i<5;i++)
    {
        String name = "Hareesh Kumar Gangadhara";
        String time = "2:53 PM";
        String date = "06/05/11";
        String time_date =time+" "+date;
        String message = "Hello Prashant";
        TableRow row_message = new TableRow(this);
        ImageView friend_img = new ImageView(this);
        LinearLayout layout_msg_info = new LinearLayout(this);
        LinearLayout layout_name_date = new LinearLayout(this);
        LinearLayout layout_message = new LinearLayout(this);

        friend_img.setLayoutParams(new LayoutParams(35,35));

        friend_img.setBackgroundResource(R.drawable.archit);

        TextView tv_name = new TextView(this);
        TextView tv_time_date = new TextView(this);
        TextView tv_message = new TextView(this);

        tv_name.setText(name);
        tv_time_date.setText(time_date);
        tv_message.setText(message);

        tv_name.setTextColor(color_black);
        tv_time_date.setTextColor(color_black);
        tv_message.setTextColor(color_black);


        tv_name.setTextSize(14);
        tv_time_date.setTextSize(10);
        tv_message.setTextSize(12);


        layout_name_date.addView(tv_name);
        layout_name_date.addView(tv_time_date);
        layout_message.addView(tv_message);

        layout_msg_info.setOrientation(LinearLayout.VERTICAL);

        layout_msg_info.setPadding(30, 10, 0, 0);
        layout_msg_info.addView(layout_name_date);
        layout_msg_info.addView(layout_message);

        row_message.setBackgroundResource(R.drawable.img_message_back);
        row_message.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT,40));
        row_message.setPadding(0, 0, 10, 5);
        row_message.addView(friend_img);
        row_message.addView(layout_msg_info);

        table_message.addView(row_message);

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扛起拖把扫天下 2024-11-26 07:16:47

使用 TableRow.LayoutParams 而不是 ViewGroup.LayoutParams

希望有帮助

Use TableRow.LayoutParams instead of ViewGroup.LayoutParams.

Hope it Helps

花间憩 2024-11-26 07:16:47

您可能需要在 ImageView 上设置 ScaleType 值。

这是 ScaleType 参考: http://developer.android.com/reference/ android/widget/ImageView.ScaleType.html

您可能希望将其设置为 FIT_XY 或 CENTER_INSIDE。

You probably need to set the ScaleType value on your ImageView.

Here's the ScaleType reference: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

You're probably looking to set it to FIT_XY or CENTER_INSIDE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文