android:TextView 的 LayoutParams 以编程方式使视图消失 [英] android: LayoutParams for TextView makes the view disappear, programmatically

查看:13
本文介绍了android:TextView 的 LayoutParams 以编程方式使视图消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从各种不同的角度来解决这个问题.基本上要点是这样的:

I've hit this from various different angles. Basically the gist is this:

我已经为需要以编程方式运行的接口设计了一个 XML 模板,因此它将在运行期间动态填充.

I've layed out a template in XML for an interface which NEEDS to be run programmatically, so it's going to be populated dynamically during the run.

这里的问题是 XML TextView 有很多布局调整(有效)并且是必要的.但如果我真的在代码中设置它们,TextView 甚至不会出现.

The problem here, is that the XML TextView has quite a few layout tweaks (which work) and are necessary. But if I actually set them in code, the TextView doesn't even show up.

(顺便说一下,TextView 嵌套在 TableRow 中,因此调用了 weight.)我首先设计的用作代码参考的 XML 模板是这样的,它工作得很好:

(The TextView, by the way, is nested inside a TableRow, thus the call to weight.) The XML template I designed first, to use as reference for the code is this, and it works just fine:

<TextView
  android:id="@+id/txtviewWhiteBox"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_weight="1.0"
  android:background="@drawable/textview_9patch_white"
  android:gravity="center"
  android:text="75"
  android:textColor="@android:color/black"
  android:layout_margin="20dp"
  android:padding="20dp"
  android:textSize="40dp" />

就像我说的那样,布局完美.但是,当我在代码中运行相同的布局并应用 LayoutParams 时,TextView 消失了.

Like I said, that lays out perfectly. When I run the same layout in code though, and apply LayoutParams, the TextView disappears.

以下是相关片段:

int textViewExampleID = 9001;

private TextView txtviewExample = new TextView(this);

private void buildTextView(){   
    LayoutParams paramsExample = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1.0f);
    txtviewExample.setId(textViewExampleID);
    txtviewExample.setBackgroundResource(R.drawable.textview_9patch_white);
    txtviewExample.setGravity(Gravity.CENTER);
    txtviewExample.setTextColor(getResources().getColor(android.R.color.black));
    paramsExample.setMargins(20, 20, 20, 20);
    txtviewExample.setPadding(20, 20, 20, 20);
    txtviewExample.setTextSize(40); 
    txtviewExample.setText("customExample");

    //if I comment out the following line, this TextView displays.
    //if I leave it in, it doesn't display.
    txtviewExample.setLayoutParams(paramsExample);
}

我意识到 LayoutParams 有各种可用的类,我一直在使用它们LinearLayout.LayoutParams、TableView.LayoutParams、RelativeLayout.LayoutParams、LayoutParams 本身...无论我尝试哪一个,任何调用setLayoutParams"的尝试都会使整个 TextView 消失.我已经搜索了这里的论坛,但还没有找到答案.这不会那么罕见.

I realize there's all sorts of available classes for LayoutParams, and I've been playing with them all LinearLayout.LayoutParams, TableView.LayoutParams, RelativeLayout.LayoutParams, LayoutParams just by itself... No matter which one I try, any attempt at calling "setLayoutParams" renders the entire TextView gone. I've scoured the forums here, and haven't quite found the answer. This can't be THAT uncommon.

推荐答案

嗯,这很痛苦,但我终于明白了.要记住的最重要的事情(我刚刚意识到)是在无数的 LayoutParams 中,您需要使用与您正在处理的视图的 PARENT 相关的一个,而不是实际视图.

well, that was painful but I finally got it figured out. The most important thing to remember (that I just realized) is that of all the myriads of LayoutParams, you need to use the one that relates to the PARENT of the view you're working on, not the actual view.

所以在我的例子中,我试图让 TextView 边距工作,但它被放在 TableRow 中.一个简单的更改是确保使用的 LayoutParams 类型是 TableRow 的类型:

So in my case, I was trying to get the TextView margins working, but it was being put inside a TableRow. The one simple change was ensuring that the type of LayoutParams being used were the ones for TableRow:

private void buildTextView(){   
    // the following change is what fixed it
    TableRow.LayoutParams paramsExample = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1.0f);
    txtviewExample.setId(textViewExampleID);
    txtviewExample.setBackgroundResource(R.drawable.textview_9patch_white);
    txtviewExample.setGravity(Gravity.CENTER);
    txtviewExample.setTextColor(getResources().getColor(android.R.color.black));
    paramsExample.setMargins(20, 20, 20, 20);
    txtviewExample.setPadding(20, 20, 20, 20);
    txtviewExample.setTextSize(40); 
    txtviewExample.setText("customExample");
    txtviewExample.setLayoutParams(paramsExample);
}

谢谢大家,希望这对其他人有用,因为我在这里的论坛中看到很多半相关的问题,但不是真正定义问题的问题.

Thanks guys, hopefully this will come in handy for somebody else down the line, as I saw a lot of semi-related questions in the forums here, but not one that really defines the problem.

这篇关于android:TextView 的 LayoutParams 以编程方式使视图消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆