android:TextView 的 LayoutParams 使视图以编程方式消失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11963465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android: LayoutParams for TextView makes the view disappear, programmatically
提问by Octoth0rpe
I've hit this from various different angles. Basically the gist is this:
我从各种不同的角度击中了这一点。基本上要点是这样的:
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 模板,因此它将在运行期间动态填充。
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.
这里的问题是 XML TextView 有很多布局调整(有效)并且是必要的。但是如果我真的在代码中设置它们,TextView 甚至不会出现。
(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 嵌套在 TableRow 内,因此调用了权重。)我首先设计的 XML 模板用作代码的参考是这样的,它工作得很好:
<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" />
Like I said, that lays out perfectly. When I run the same layout in code though, and apply LayoutParams, the TextView disappears.
就像我说的,这完美地布局。但是,当我在代码中运行相同的布局并应用 LayoutParams 时,TextView 消失了。
Here's the relevant snippet:
这是相关的片段:
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);
}
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 有各种可用的类,而且我一直在使用它们所有的 LinearLayout.LayoutParams、TableView.LayoutParams、RelativeLayout.LayoutParams、LayoutParams 本身......无论我尝试哪一个,任何尝试调用“setLayoutParams”使整个 TextView 消失了。我在这里搜索了论坛,还没有找到答案。这不可能那么罕见。
回答by Octoth0rpe
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.
好吧,那很痛苦,但我终于弄明白了。要记住的最重要的事情(我刚刚意识到)是所有无数的LayoutParams
,您需要使用与您正在处理的视图的 PARENT 相关的一个,而不是实际视图。
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
:
所以就我而言,我试图让TextView
边距发挥作用,但它被放在TableRow
. 一个简单的变化是确保使用的类型LayoutParams
是用于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 lotof semi-related questions in the forums here, but not one that really defines the problem.
谢谢大家,希望这对其他人有用,因为我在这里的论坛中看到了很多半相关的问题,但没有一个真正定义问题。
回答by killer
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");
setContentView(txtviewExample);//when you don't use SETTER method for TextView you can't view the desireable text on the UI//
}
//use
//用
回答by CQM
what is the 3rd variable in layoutparams supposed to do, is that alpha? what happens if you comment out paramsExample.setMargins
layoutparams 中的第三个变量应该做什么,是 alpha 吗?如果你注释掉会发生什么paramsExample.setMargins
finally what happens if you to txtviewExample.setVisible(View.Visible)
after you setLayoutParams
?
最后如果你txtviewExample.setVisible(View.Visible)
追你会setLayoutParams
怎样?
those would be the things I would try if you haven't
如果你没有,这些就是我会尝试的事情