开发者

Main.xml in Android

开发者 https://www.devze.com 2023-01-17 09:44 出处:网络
Hello I have a bunch of text on my file which i display. I used the Hello World demo to insert this text. Now Whenever i try to edit main.xml it doesn\'t happen. If anybody knows why that would be coo

Hello I have a bunch of text on my file which i display. I used the Hello World demo to insert this text. Now Whenever i try to edit main.xml it doesn't happen. If anybody knows why that would be cool but I want to know how to add simple scrolling to my text. I don't think I need to use main.xml right. Is there a way I can just add it to my project?

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class manifesto extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //Make a new text view passing Activity开发者_如何学Go object
    TextView tv = new TextView(this);
    //Set a text into view

    tv.setText("A bunch of text.");
    tv.setTextSize(12);

    //set the view into activity view container
    setContentView(tv);    
    }
}    

Here is the XML file in case you want to see it.

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

<TextView android:text="@+id/TextView01" 
android:layout_width="wrap_content"
 android:layout_height="wrap_content" 
 android:id="@string/hello"></TextView>                        

 </LinearLayout>


First off, you're calling setContentView(tv), so you're not actually seeing any Views from the XML, only the TextView you initialized in onCreate(). Also, it looks like you've got your android:text and android:id attributes mixed up in your XML. It should be

<TextView android:id="@+id/TextView01" 
  android:layout_width="wrap_content" android:layout_height="wrap_content" 
  android:text="@string/hello" /> 

And for your question on scrolling, see Making TextView scrollable on Android.


Did you realize what when wrong with your code and why it did not work in first place. Here is the step by step analysis.

1) You have here set the layout to the screen in first setp setContentView(R.layout.main);

2) This step is not necessary.

////*****
//Make a new text view passing Activity object
TextView tv = new TextView(this);
//Set a text into view

tv.setText("A bunch of text.");
tv.setTextSize(12);   *****////

3) You actually overwrote your layout with your newly created object which was incorrect. //set the view into activity view container setContentView(tv);

4) Lastly you messed up the ids,

5) MY suggestion is please read topics on view and layouts and their differences and instead of just putting and running the code try to understand the finer aspects of it.

Thanks and best regards Prasad


The set on the content view was always reset to tv,

 setContentView to (R.layout.main to reflect the changes in main.xml
0

精彩评论

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

关注公众号