三种方法在Android界面显示文本

详细解释案例

方法一

package com.thundersoft.myapplication.demo0525;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.thundersoft.myapplication.R;

/*
 *使用XML控制布局
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //使当前Activity 与 Layout 进行关联
        setContentView(R.layout.activity_layout);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="使用XML控制布局" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1. 将布局标签改为 LinearLayout" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2. 在LinearLayout下添加android:orientation为vertical垂直方向" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3. 要添加一段文本,就添加一个TextView" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="4. AndroidManifest.xml中确保使用的是当前的activity,即其他activity的intent-filter不会干扰主activity" />
</LinearLayout>

方法二

package com.thundersoft.myapplication.demo0525;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

/*
 *使用Java代码控制布局
 */

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //新建LinearLayout父布局
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        //新建textView组件
        TextView textView1 = new TextView(this);
        //为textView组件设置文字
        textView1.setText("仅使用Java代码控制布局");

        TextView textView2 = new TextView(this);
        textView2.setText("1.new 新建LinearLayout父布局");

        TextView textView3 = new TextView(this);
        textView3.setText("2.使用布局对象名.setOrientation(LinearLayout.VERTICAL) 方法,设置布局方向等");

        TextView textView4 = new TextView(this);
        textView4.setText("3.new 新建TextView组件,通过TextView组件名.setText()设置组件文本内容");

        TextView textView5 = new TextView(this);
        textView5.setText("4.使用布局对象名.addView()方法将TextView组件对象加入到父布局中");

        TextView textView6 = new TextView(this);
        textView6.setText("5.使用setContentView(布局对象名)方法,将当前activity与在Java文件中创建的Layout布局相关联");

        TextView textView7 = new TextView(this);
        textView7.setText("6.确保清单文件Manifest文件中的主activity为当前activity");

        //将textView1组件加入到父布局
        linearLayout.addView(textView1);
        linearLayout.addView(textView2);
        linearLayout.addView(textView3);
        linearLayout.addView(textView4);
        linearLayout.addView(textView5);
        linearLayout.addView(textView6);
        linearLayout.addView(textView7);

        //使当前Activity 与 新建的Layout 进行关联
        setContentView(linearLayout);
    }
}

方法三

package com.thundersoft.myapplication.demo0525;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.thundersoft.myapplication.R;

/*
 *使用XML和Java控制布局
 */
public class MainActivity3 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout3);
        //从关联的布局中通过findViewById()找到父布局
        LinearLayout linearLayout = findViewById(R.id.line);
        //新建TextView组件
        TextView textView1 = new TextView(this);
        textView1.setText("5.从关联的布局中通过findViewById()方法、在xml中设置的布局名id找到父布局");

        TextView textView2 = new TextView(this);
        textView2.setText("6.新建TextView组件,并使用组件对象名.setText()添加文本");

        TextView textView3 = new TextView(this);
        textView3.setText("7.使用布局对象名.addView()方法将TextView组件加入父布局");

        TextView textView4 = new TextView(this);
        textView4.setText("8.确保清单文件AndroidManifest文件中的主activity为当前想要运行的activity");
        //将TextView加入父布局
        linearLayout.addView(textView1);
        linearLayout.addView(textView2);
        linearLayout.addView(textView3);
        linearLayout.addView(textView4);

    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/line"
    tools:context=".demo0525.MainActivity3">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="使用XML和Java控制布局"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1. 在activity_layout中设置布局为LinearLayout"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2. 设置当前LinearLayout的id为android:id=@+id/line确保Java可以调用xml中的设置,例方向"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3. 在LinearLayout下设置布局方向为垂直方向vertical"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="4. 以下由Java进行编写"/>
</LinearLayout>

简化版案例

以呈现出垂直方法布局的三行文本为例

方法一

只在xml里面进行设置
三行均由xml设置 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<!--目标效果:垂直布局
1. <LinerLayout    android:orientation="vertical" />
2. 添加多段文本 一段文本使用一个<TextView  />
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I am the first"
        />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I am the second"
        />

</LinearLayout>

方法二

只在Java类里面进行设置
三行均由Java设置 MainActivity

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);

        TextView textView1=new TextView(this);
        textView1.setText("This is the first");

        TextView textView2=new TextView(this);
        textView2.setText("This is the second");

        linearLayout.addView(textView1);
        linearLayout.addView(textView2);

        setContentView(linearLayout);

    }

}

方法三

在xml和Java中混合设置
一行由xml设置,两行由Java设置
activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2"
    android:orientation="vertical"

    >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    />
    
</LinearLayout>

MainActivity2

package com.example.myapplication;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        LinearLayout linearLayout = findViewById(R.id.linear);

        TextView textView1 = new TextView(this);
        textView1.setText("This is the first");

        TextView textView2 = new TextView(this);
        textView2.setText("This is the second");

        linearLayout.addView(textView1);
        linearLayout.addView(textView2);

    }
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值