2012-08-15 42 views
22

我从各个不同的角度打这个。基本上主要是这样的:android:用于TextView的LayoutParams使视图消失,以编程方式

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

这里的问题是,XML TextView有相当多的布局调整(工作),是必要的。但是,如果我真的把它们设置在代码中,TextView甚至不会显示出来。 (这个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" /> 

就像我说的那样,完美地勾勒出来。 当我在代码中运行相同的布局,并应用LayoutParams时,TextView消失。

下面是相关的代码片段:

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消失。 我在这里搜索论坛,并没有找到答案。这并不罕见。

+4

你试过'TableRow.LayoutParams' http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html – 2012-08-15 04:10:16

+1

我只是做了,(错过了这个评论昨晚,达尼特),它工作。 谢谢你 – Octoth0rpe 2012-08-15 14:43:49

+0

不客气:) – 2012-08-15 14:51:23

回答

84

好,我会尝试,这是痛苦的,但是我终于想通了。 要记住的最重要的事情(我刚刚意识到)是所有无数的LayoutParams,您需要使用与您正在处理的视图的父项相关的项目,而不是实际的视图。

所以在我的情况下,我试图让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); 
} 

谢谢你们,希望这能派上用场了别人的路线,因为我看到的很多在这里的论坛中与半相关的问题,但不是真正定义问题的问题。

+5

谢谢!我有同样的问题,这是因为我使用LinearLayout.LayoutParams而不是TableRow.LayoutParams ...愚蠢的错误,但你救了我很多挫折! – RyanG 2012-11-29 16:25:45

+0

我只想说谢谢!你救了我的一天! – Zookey 2014-05-04 15:19:52

+3

两个小时我试图解决这个问题。谢谢你,兄弟。 – arenaq 2014-11-24 12:47:19

0

什么是layoutparams应该做的第三个变量是alpha?如果你发表评论,会发生什么情况paramsExample.setMargins

终于会发生什么,如果你在txtviewExample.setVisible(View.Visible)之后setLayoutParams

这些将是东西,如果你还没有

+0

layoutparams中的第三个参考是为了体重,相当于 'android:layout_weight =“1.0”' 部分在XML中。和'.setVisible(View.VISABLE);'不起作用。谢谢,不过。 – Octoth0rpe 2012-08-15 02:51:09

+0

FWIW,我也尝试过不使用'txtviewExample.setLayoutParms(paramsExample);'然后在布局中的实际addView期间,将它指定为辅助参数。仍然没有工作:'tblrow.addView(txtviewExample,paramsExample);'' – Octoth0rpe 2012-08-15 03:02:42

+0

in'setText'我假设你有一个实际的字符串变量?它被调用时是否有任何数据? – CQM 2012-08-15 03:09:30

1
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// 
} 

//使用

+0

使用setContentView(txtviewExample)在UI屏幕上显示文本 – killer 2013-07-24 04:59:43

+0

不能只是评论而不是回答已经回答的问题。 – NREZ 2013-07-24 05:22:04

相关问题