我从各个不同的角度打这个。基本上主要是这样的: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消失。 我在这里搜索论坛,并没有找到答案。这并不罕见。
你试过'TableRow.LayoutParams' http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html – 2012-08-15 04:10:16
我只是做了,(错过了这个评论昨晚,达尼特),它工作。 谢谢你 – Octoth0rpe 2012-08-15 14:43:49
不客气:) – 2012-08-15 14:51:23