0

我编写了一个简单的程序来熟悉 android 中的 UI 设计。我在线性布局中动态添加了文本。当我调试代码时,我在 logcat 中看不到任何错误,代码运行得很好。但我无法在我的设备中看到任何文字。我正在使用 Galaxy S2。我也尝试过使用模拟器。但什么都没有出现。我不知道为什么它没有在我的设备/模拟器中显示任何文本。请帮忙。

这是我的活动

public class ViewActivity extends Activity {
/** Called when the activity is first created. */
LinearLayout bar;
String myarray[]={"Foot Ball","Basket Ball","Cricket"};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bar= (LinearLayout) findViewById(R.id.tag);// Map the Layout
bar.setVisibility(View.VISIBLE);
TextView bs_text[]= new TextView[myarray.length]; // Create TextView according to value in length variable;

for(int z=0;z< myarray.length ;z++)
{
try
{

bs_text[z] = (TextView) new TextView(this);
bs_text[z].setText( myarray [z]);
//bs_text[z].setTextSize(1);
bs_text[z].setBackgroundColor(android.graphics.Color.TRANSPARENT);
bs_text[z].setTextColor(android.graphics.Color.BLACK);
int height_in_pixels = bs_text[z].getLineCount() * bs_text[z].getLineHeight(); //approx height text
bs_text[z].setHeight(height_in_pixels);
//bs_text[z].setHeight(android.graphics.)
bar.addView(bs_text[z]);
}

catch(ArrayIndexOutOfBoundsException e)
{
Log.d("ArrayIndexOutOfBoundsException",e.toString());
}
}
}
    }

这是我的 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tag"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</LinearLayout>
4

4 回答 4

3

将 layoutparams 设置为 textview 为:

bs_text[z].setBackgroundColor(0xffffffff);
bar.addView(bs_text[z],new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
于 2012-04-24T04:05:20.157 回答
0

尝试将 layoutparams 设置为 textview

LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
textview.setLayoutParams(lp);
于 2012-04-24T04:01:01.863 回答
0

当您第一次显示它时,您可能LinearView会显示为width=0 和=0:height

android:layout_width="wrap_content"
android:layout_height="wrap_content"

由于它没有内容,因此大小为 0。您应该重置视​​图,例如:

LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
bar.setLayoutParams(lp);

添加 TextViews 后

于 2012-04-24T04:03:52.797 回答
0

如果你想给 TextViews 赋予特定的权重,那么你可以设置布局参数如下 -

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,1.0f);

哪里1.0f是显示的重量TextView。然后将参数添加到TextView

bs_text[z].setLayoutParams(param);
于 2013-06-05T06:48:55.327 回答