android textview setlayoutparams programmatically
XML File
<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" />
Java File
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); }
New Java File
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); }
'안드로이드 개발 팁' 카테고리의 다른 글
android 유투브 강의 (0) | 2014.06.12 |
---|---|
android 페이지 목록 동적으로 처리하기 (0) | 2014.05.29 |
android dp, px 해상도 변환 및 폰 해상도 구하기 (0) | 2014.05.28 |
Android Custom Keyboard 예제2 (0) | 2014.05.20 |
Android Custom Keyboard 예제 (2) | 2014.05.19 |