开发者

Android:Drawing Many Text Views

I'm new to android platform , I'd like to settext using textviews , I tried to write set text into two textviews but its just draws one textview why? I can't draw two textviews

TextVi开发者_StackOverflowew tv1;
TextView tv2;

@Override

public void onCreate(Bundle savedInstanceState)

{

     super.onCreate(savedInstanceState);

     layout = new LinearLayout(this);

     tv1 = new TextView(this);

     tv2 = new TextView(this);
     tv1.setText("Hello");

     tv2.setText("How are you?");

}


On Android, the user interface normally should be created using XML-files, instead of Java code. You should read up on the tutorials on android.com, especially:

http://developer.android.com/guide/topics/ui/declaring-layout.html

An example:

In your res/layout/main.xml, you define the text TextView's:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <TextView android:id="@+id/TextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView 1"/>
 <TextView android:id="@+id/TextView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView 2"/> 
</LinearLayout>

Then if you use setContentView in the activity to display this, the app will show to TextView's:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);
}

If you want to programmatically set the text in the Activity, just use findViewById():

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);
    ((TextView)this.findViewById(R.id.TextView1)).setText("Setting text of TextView1");
}


I definitely second TuomasR's suggestion to use XML layouts. However, if you're wanting to add new TextViews dynamically (i.e. you don't know how many you will need until runtime), you need to do a couple of other steps to what you are doing:

First, define your LinearLayout in main.xml (it's just easier that way than LayoutParams, IMO):

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

Now, you can go to your code, and try the following:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    //This inflates your XML file to the view to be displayed.
    //Nothing exists on-screen at this point
    setContentView(R.layout.main);

    //This finds the LinearLayout in main.xml that you gave an ID to
    LinearLayout layout = (LinearLayout)findViewById(R.id.my_linear_layout);

    TextView t1 = new TextView(this);
    TextView t2 = new TextView(this);

    t1.setText("Hello.");
    t2.setText("How are you?");

    //Here, you have to add these TextViews to the LinearLayout
    layout.addView(t1);
    layout.addView(t2);

    //Both TextViews should display at this point
}

Again, if you know ahead of time how many views that you need, USE XML.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜