【问题标题】:Table layout adding image and textview into a row side by side pragmatically表格布局实用地将图像和文本视图并排添加到行中
【发布时间】:2015-06-19 05:47:59
【问题描述】:

我正在尝试做一些简单的事情,但这让我非常心痛。我试图务实地创建一个表格布局,添加一行,插入一个图像和一个文本视图。图像应该占据行的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);

【问题讨论】:

    标签: android android-layout xamarin android-tablelayout


    【解决方案1】:

    为什么你不使用线性布局或相对布局,它会更好地满足你的要求。检查下面的线性布局示例。

            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);
    

    【讨论】:

      【解决方案2】:

      我想使用带有 weightSum 属性的 LinearLayout。对应的 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 占用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-06
        • 2012-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多