创建动态表格布局时出现问题
我创建了一个动态 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
TableRow.LayoutParams
而不是ViewGroup.LayoutParams
。希望有帮助
Use
TableRow.LayoutParams
instead ofViewGroup.LayoutParams
.Hope it Helps
您可能需要在 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.