安卓的LayoutParams为的TextView使视图中消失,编程 [英] android: LayoutParams for TextView makes the view disappear, programatically

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

问题描述

我已经从各种不同的角度打这个。基本上要旨是这样的:

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有不少布局的调整(这工作),并是必要的。但是,如果我实际设置在code,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,从而调用重。) XML模板我设计的第一个,作为code参考用的就是这个,它工作得很好:

(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" />

就像我说的,是勾画出完美。 当我运行在code相同的布局虽然和适用的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.
Any ideas folks?

推荐答案

好了,这是痛苦的,但我终于想通了。 要记得(我才意识到)最重要的是,的LayoutParams 的一切,你需要使用涉及视图你的父一个千变万化的重新工作的,而不是实际的图。

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.

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

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