如何在android中设置背景图像

前端之家收集整理的这篇文章主要介绍了如何在android中设置背景图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是这个 Android开发的新手.我刚刚开始创建一个hello world应用程序.

我试图设置一个背景图像,它工作正常.当我尝试在图像上添加文本时,它无法正常工作.

我尝试了下面的方式.任何人都可以帮助我,我出错了.

main.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" 
    android:background="@drawable/andriod" 
    android:layout_gravity="center_vertical" 
    android:gravity="center_vertical"> 

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

HelloActivity.java

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

    public class HelloActivity extends Activity {
    //    /** Called when the activity is first created. */
    //    @Override
    //    public void onCreate(Bundle savedInstanceState) {
    //        super.onCreate(savedInstanceState);
    //        setContentView(R.layout.main);
    //    }    

    /** Called when the activity is first created. */
       @Override

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

           TextView tv = new TextView(this);              
           tv.setText("This is the first andorid app");          
           setContentView(R.layout.main);         
       }
}

在运行时显示背景图像,但“这是第一个andorid应用程序”未显示.

希望得到你的帮助

解决方法

您必须使用其唯一ID在布局文件中选择TextView,然后设置其属性.但是在你的代码中,你只需创建一个带有文本的文本视图,而不是将其放入视图中.
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("Your text here");
原文链接:https://www.f2er.com/android/315689.html

猜你在找的Android相关文章