如何从主活动更改另一个 XML 文件中 TextView 的重力?

huangapple go评论60阅读模式
英文:

How to change the gravity of a TextView in another xml file from Main Activity?

问题

我有一个XML文件,其中有一个TextView,我想从主活动更改它的对齐方式。我在主活动中使用了以下代码:

// 我只复制了相关的代码

TextView tvValue;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvValue = findViewById(R.id.tv_value);

    // the tv_value id is from the (word_item.xml) file
}

private void OnClickFunc(View view) {
    tvValue.setGravity(Gravity.RIGHT);
}

问题是我得到了一个NullPointerException错误,指向一个null对象引用。我知道我应该以某种方式将xml文件与主活动连接起来。

word_item.xml文件:

<?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="wrap_content"
android:orientation="vertical"
android:padding="10dp">

  <TextView
    android:id="@+id/tv_value"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Word" />

</LinearLayout>

谢谢,非常感谢您的帮助。

英文:

I have an XML file that has a TextView, and I want to change its gravity from the main activity.
I used this code in the main activity:

// I just coppied the related codes 

TextView tvValue;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvValue = findViewById(R.id.tv_value); 

    // the tv_value id is from the (word_item.xml) file

}

private void OnClickFunc(View view) {
    tvValue.setGravity(Gravity.RIGHT);
}

and the problem is that I get a NullPointerException error on a null object reference.
I know that I should somehow connect the xml file with the main activity.

word_item.xml file:

<?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="wrap_content"
android:orientation="vertical"
android:padding="10dp">

  <TextView
    android:id="@+id/tv_value"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Word" />

</LinearLayout>

Thank you and I appreciate your help.

答案1

得分: 1

if you havent included the word_item.xml in your activity_main.xml then the error is obvious

so this is how you can include another file in any xml file in android

<include layout="@layout/word_item.xml" />

then things ll work as you expect

英文:

if you havent included the word_item.xml in your activity_main.xml then the error is obvious

so this is how you can include another file in any xml file in android

&lt;include layout=&quot;@layout/word_item.xml&quot; /&gt;

then things ll work as you expect

答案2

得分: 1

尝试首先按照以下方式填充另一个布局,然后初始化视图:

LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
final View v = inflater.inflate(R.layout.word_item, null);

然后:

Textview tvValue = findViewById(R.id.tv_value);
private void OnClickFunc() {
tvValue.setGravity(Gravity.RIGHT);
英文:

Try to first inflate the other layout as follows the initialize the view

LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
final View v = inflater.inflate(R.layout.word_item, null);

Then

Textview tvValue = findViewById(R.id.tv_value);
private void OnClickFunc() {
tvValue.setGravity(Gravity.RIGHT);

答案3

得分: 0

你在哪里初始化了textView变量?变量必须在Activity的OnCreate方法中的setContentView之后进行初始化。

英文:

Where did you initialize textView variable?? Variable must be initialize after setContentView in OnCreate method in Activity.

huangapple
  • 本文由 发表于 2020年10月12日 01:19:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64306959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定