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.
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.
(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" />
Like I said, that lays out perfectly.
When I run the same layout in code though, and apply LayoutParams, the TextView disappears.
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.
See Question&Answers more detail:
os