开发者

How to link the textview to my XML layout

im making a random fact generator and ive got the code sorted but i want it to appear with my layout in the xml file, the code i have is as follows

public class facts extends Activity {

//implements android.view.View.OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layou开发者_如何学Ct.facts);
    setTitle("Buy - Find a carpool!");



    Random generator= new Random();
    int num;
    String[] quotes;
    quotes= new String[15];
    quotes[0]= "test1)";
    quotes[1]= "test2";
    TextView tv = new TextView(this);
    num=generator.nextInt(15);
    tv.setText(quotes[num]);
    setContentView(R.layout.facts);

My XML code is

      <TextView
 android:gravity="center_vertical|center_horizontal"
 android:id="@+id/factslist"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android.layout_column="15" 
      android:layout_centerVertical="true"
  android:layout_centerHorizontal="true"/>

Does anyone know how to do this?


Look at this: Declaring Layout. You have to use findViewById() to get an instance of your TextView. Don't instantiate it yourself. And also, you don't have to setContentView again after changing things.


You put the TextView in your facts.xml file and then use...

TextView tv = (TextView) findViewById(R.id.myTextView);


Why do you create a TextView in code:

TextView tv = new TextView(this);

Why not have a TextView in your .xml file (facts.xml)? Perhaps I don't understand your question. Can you clarify what you mean by "i want it to appear with my layout in the xml file".

If you mean you want the TextView defined in .xml, then add a <TextView> element to the file facts.xml:

   <xml version="1.0" encoding="utf-8">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent" 
              android:orientation="vertical" >
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello, I am a TextView" />
</LinearLayout>

So instead of this code:

Random generator= new Random();
int num;
String[] quotes;
quotes= new String[15];
quotes[0]= "test1)";
quotes[1]= "test2";
TextView tv = new TextView(this);
num=generator.nextInt(15);
tv.setText(quotes[num]);
setContentView(R.layout.facts);

You should have:

Random generator= new Random();
int num;
String[] quotes;
quotes= new String[15];
quotes[0]= "test1)";
quotes[1]= "test2";

TextView tv = (TextView) findViewById(R.id.factslist);

num=generator.nextInt(15);
tv.setText(quotes[num]);

Do you see how you are obtaining a reference to the TextView in you .xml file based in its id, and then setting the value of this id? I also don't think you need to call this twice:

setContentView(R.layout.facts);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜