【问题标题】:Stretch TextView Width and Height equal to GridLayout's Cell Width Height拉伸 TextView 的宽度和高度等于 GridLayout 的单元格宽度高度
【发布时间】:2015-07-11 00:50:51
【问题描述】:

我正在尝试在运行时将 TextView 添加到 GridLayout。由于没有设置 GridLayout 的单元格背景的属性,所以我设置 TextView 背景看起来像单元格背景在其边界处有笔划。我的问题是,在运行时将 TextView 添加到 Gridlayout 时, TextView 大小不 Stretching 等于 GridLayout Cell 。

在我设置的xml文件中:

<GridLayout
    android:id="@+id/gridLay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:alignmentMode="alignBounds"
    android:columnCount="5"
    android:columnOrderPreserved="false"
    android:useDefaultMargins="false" >

    <TextView
        android:layout_gravity="fill"
        android:background="@drawable/grid_lay_cell_bg"
        android:padding="5dp"
        android:text="Name"
        android:textSize="14dp" />

</GridLayout>

它工作得很好,TextView 涵盖了完整的单元格大小,但是当我尝试在以下代码中添加它时:

TextView tv3 = new TextView(getActivity());
    GridLayout.LayoutParams lp3 = new GridLayout.LayoutParams(GridLayout.spec(2, 1), GridLayout.spec(0, 1));
    lp3.setGravity(Gravity.FILL);
    tv3.setLayoutParams(lp3);
    tv3.setGravity(Gravity.FILL);
    tv3.setPadding(padd, padd, padd, padd);
    tv3.setBackgroundResource(R.drawable.grid_lay_cell_bg);
    tv3.setTextSize(16);
    tv3.setText("Test");
    gridLay.addView(tv3, lp3);

TextView 没有覆盖完整的单元格大小。

请给我一些提示,在Android运行时将TextView添加到GridLayout时如何拉伸TextView大小等于单元格大小。

【问题讨论】:

标签: android textview android-gridlayout


【解决方案1】:

试试这个

TextView textView = (TextView)findViewById(R.id.text_view);
ViewGroup.LayoutParams params = textView.getLayoutParams();
params.width = ViewGroup.LayoutParams.FILL_PARENT;
textView.setLayoutParams(params);

【讨论】:

  • 我已经尝试过了:GridLayout.LayoutParams lp = new GridLayout.LayoutParams(); lp.width = LayoutParams.FILL_PARENT; lp.height = LayoutParams.FILL_PARENT; lp.setGravity(Gravity.FILL); tv.setLayoutParams(lp);但没有运气:'(
  • @ Murtaza Khursheed Hussain 我已经更新了我的问题并添加了我的问题的屏幕截图。
  • gridlayout 已弃用,最好是您可以使用带有 TextView 的 LinearLayout,其宽度设置为 0dp,重量设置为 0.25,这样您可以拥有等宽的框。
  • 我需要使用 col span 和 row span 属性,这就是我使用 GridLayout 的原因
猜你喜欢
  • 2014-01-22
  • 2010-12-24
  • 1970-01-01
  • 2014-11-11
  • 1970-01-01
  • 2015-09-03
  • 2020-06-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多