我试图做一些简单的事情,但它给了我很多心痛。我试图以实用方式创建表格布局,添加行,插入图像和文本视图。该图片应该占据该行的25%
,其余部分应为TextView
。我没有被TableLayout
困住。如果你认为某些事情变得更好,我会全力以赴,但请它必须是动态/实用的。在任何帮助研究员之前都要感谢!
这是我尝试过的。当我运行它时,没有任何显示。我有一个LinearLayout
(ll),我试图将表格添加到:
TableLayout tl = new TableLayout(this);
TableRow tr = new TableRow(this);
//add user avatar image
ImageView uivd = new ImageView (this);
param = new LinearLayout.LayoutParams (175,
175);
uivd.Id = idi++;
uivd.SetMaxWidth(175);
uivd.SetMaxHeight(175);
uivd.SetPadding (10, 10, 10, 10);
var vimageBitmap = GetImageBitmapFromUrl (objModel.BaseURL + item.AuthorAvatar);
uivd.SetImageBitmap (vimageBitmap);
param.SetMargins (10, 10, 10, 10);
//add the image to the table row
tr.AddView(uivd,0,param);
//add the textview should take 75% of the width
TextView v = new TextView (this);
v.Id = idi++;
param = new LinearLayout.LayoutParams (ViewGroup.LayoutParams.WrapContent,
ViewGroup.LayoutParams.WrapContent);
param.SetMargins (15, 15, 15, 15);
v.SetBackgroundColor (Color.White);
v.SetTextColor (Color.Black);
v.SetTextSize (Android.Util.ComplexUnitType.Pt, 10);
v.SetBackgroundDrawable (Resources.GetDrawable (Resource.Drawable.textrounded));
v.Text = item.Title +
System.Environment.NewLine +item.Msg;
//add the textview to the table row
tr.AddView (v, 1, param);
//add the table row to the table
tl.AddView (tr);
//add the table to the Linearlayout at the index idx
ll.AddView (tl, idx++, param);
答案 0 :(得分:0)
为什么不使用线性布局或相对布局,最好满足您的要求。使用线性布局检查以下示例。
LinearLayout parentlayout = (LinearLayout)findViewById(R.id.ll);
LinearLayout childLayout = new LinearLayout(this);
childLayout.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
childLayout.setOrientation(LinearLayout.HORIZONTAL);
TextView tv = new TextView(this);
tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.75f));
tv.setTextColor(Color.BLACK);
ImageView iv = new ImageView(this);
iv.setBackgroundResource(R.drawable.icon);
iv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.25f));
tv.setText("This is the Text ");
tv.setTextSize(18.0f);
childLayout.addView(iv);
childLayout.addView(tv);
parentlayout.addView(childLayout);
答案 1 :(得分:0)
我想使用LinearLayout和weightSum属性。对应的xml是。您可以将其更改为Java。虽然这个概念使用相同的。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.75"/>
</LinearLayout>
这里LinearLayout的weightSum是1.而ImageView的layout_weight是0.25。所以它占据了25%的空间。其余的75%将由TextView采用。