以编程方式将 TextView 设置为 RelativeLayout.CENTER_OF_PARENT
Programmatically setting TextView to RelativeLayout.CENTER_OF_PARENT
我正在学习 Java 和 XML 并且正在尝试将 TextView 设置在其父 RelativeLayout 的中心。我的应用程序仅在我注释掉 setContentView(homeScreen)
之前的最后 3 行时加载
这是我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</RelativeLayout>
这是我的 Java:
package com.example.android.testerapp1;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView homeScreen = new TextView(this);
homeScreen.setText("Welcome to Test App 001" + "\nThis TextView was created dynamically in Java!");
homeScreen.setTextSize(24);
homeScreen.setTextColor(Color.CYAN);
homeScreen.setCursorVisible(true);
homeScreen.setPadding(16,56,16,56);
homeScreen.setBackgroundColor(Color.BLACK);
homeScreen.setGravity(Gravity.CENTER);
//dynamically set width to dp (converted to pixels ~600) and height to 'wrap content'
// convert dp amount to pixels for size
final float scale = getResources().getDisplayMetrics().density;
int pixelWidth = (int) (2000 / scale + 0.5f);
homeScreen.setLayoutParams(new ViewGroup.LayoutParams(pixelWidth , ViewGroup.LayoutParams.WRAP_CONTENT));
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)homeScreen.getLayoutParams();
params.addRule(RelativeLayout.CENTER_IN_PARENT);
homeScreen.setLayoutParams(params);
setContentView(homeScreen);
}
}
我已经看到这种 post 大约 10 次了,它们都有相同的解决方案,但我似乎无法正确实施,它可能是我代码的另一部分?可能在我设置宽度和高度的地方使用setLayoutParams
also?
您可以在构造函数上设置宽度和高度,然后使用它
Relative.LayoutParams(int width, int height)
所以你需要这样做:
homeScreen.setLayoutParams(width , height);
setContentView()
调用应该是用来设置全屏布局的。您目前在 Activity 代码中所做的只是将 TextView 设置为屏幕的完整视图,因此 Activity 没有引用您创建的 XML 布局。这就是为什么最后的 3 行代码失败的原因,因为 TextView
正在尝试设置其 LayoutParams
以决定其父级应该如何放置和测量它,但是在这种情况下它没有父级。我建议做的是给 XML 中的 RelativeLayout
一个 id
属性,以便在 Activity
代码中获得对它的引用,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="home_screen_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"/>
然后在您的 Activity
代码中,调整它以便您使用 XML 文件的资源 ID 进行调用。如果我们假设它在您的资源目录的布局文件夹中称为 act_main.xml
(即在 src/main/resources/layout/act_main.xml
中),您将调用 setContentView(R.layout.act_main)
作为 onCreate()
中 [=] 之后的第一行23=] 调用,以便框架有机会解析您的 XML 并对其进行膨胀(即实例化,计算大小和
确定其组件的放置等)。之后,使用 findViewById(R.id.home_screen_layout)
获取对 RelativeLayout
的引用,以便您可以创建一个新的 TextView
并将其添加到您已经膨胀的布局中。
package com.example.android.testerapp1;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// make your view components private members as findViewById calls are expensive for the framework
private RelativeLayout homeScreenLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Have the activity inflate the XML file with your RelativeLayout
setContentView(R.layout.act_main);
// Now that it is inflated, get a reference to that parent
homeScreenLayout = (RelativeLayout) findViewById(R.id.home_screen_layout);
// Dynamically create a TextView associated with this Activity's context
TextView homeScreen = new TextView(this);
homeScreen.setText("Welcome to Test App 001" + "\nThis TextView was created dynamically in Java!");
homeScreen.setTextSize(24);
homeScreen.setTextColor(Color.CYAN);
homeScreen.setCursorVisible(true);
homeScreen.setPadding(16,56,16,56);
homeScreen.setBackgroundColor(Color.BLACK);
homeScreen.setGravity(Gravity.CENTER);
//dynamically set width to dp (converted to pixels ~600) and height to 'wrap content'
// convert dp amount to pixels for size
final float scale = getResources().getDisplayMetrics().density;
int pixelWidth = (int) (2000 / scale + 0.5f);
// Adjust the placement in the parent
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(pixelWidth , RelativeLayout.LayoutParams.WRAP_CONTENT)
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); // make sure to use the function which takes a boolean value for rules like CENTER_IN_PARENT
homeScreen.setLayoutParams(params); // Add these parameters to the textview
// Let the layout know about your newly created textview so that it can re-draw its canvas
homeScreenLayout.addView(homeScreen);
}
}
请注意,我要补充一点,您可以在 XML 中相对轻松地完成您正在做的事情,但由于您询问了具体的编程设置,我不会深入探讨这方面的细节。但如果您对某些结构化资源感兴趣,我建议您查看 Android Developer Guide, specifically the section on XML layouts and how they interact with Activities
编辑: 请注意我对 Activity 代码所做的更改。主要部分是首先用 setContentView(int id)
膨胀空的 RelativeLayout xml,然后将另一个 TextView
添加到给定的布局。我提供的有关 CENTER_IN_PARENT
行的代码存在一个小错误。根据 [docs](https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#addRule(int, int)),添加使用布尔值的规则时必须使用函数的 addRule(int, int)
版本。
我正在学习 Java 和 XML 并且正在尝试将 TextView 设置在其父 RelativeLayout 的中心。我的应用程序仅在我注释掉 setContentView(homeScreen)
这是我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</RelativeLayout>
这是我的 Java:
package com.example.android.testerapp1;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView homeScreen = new TextView(this);
homeScreen.setText("Welcome to Test App 001" + "\nThis TextView was created dynamically in Java!");
homeScreen.setTextSize(24);
homeScreen.setTextColor(Color.CYAN);
homeScreen.setCursorVisible(true);
homeScreen.setPadding(16,56,16,56);
homeScreen.setBackgroundColor(Color.BLACK);
homeScreen.setGravity(Gravity.CENTER);
//dynamically set width to dp (converted to pixels ~600) and height to 'wrap content'
// convert dp amount to pixels for size
final float scale = getResources().getDisplayMetrics().density;
int pixelWidth = (int) (2000 / scale + 0.5f);
homeScreen.setLayoutParams(new ViewGroup.LayoutParams(pixelWidth , ViewGroup.LayoutParams.WRAP_CONTENT));
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)homeScreen.getLayoutParams();
params.addRule(RelativeLayout.CENTER_IN_PARENT);
homeScreen.setLayoutParams(params);
setContentView(homeScreen);
}
}
我已经看到这种 post 大约 10 次了,它们都有相同的解决方案,但我似乎无法正确实施,它可能是我代码的另一部分?可能在我设置宽度和高度的地方使用setLayoutParams
also?
您可以在构造函数上设置宽度和高度,然后使用它
Relative.LayoutParams(int width, int height)
所以你需要这样做:
homeScreen.setLayoutParams(width , height);
setContentView()
调用应该是用来设置全屏布局的。您目前在 Activity 代码中所做的只是将 TextView 设置为屏幕的完整视图,因此 Activity 没有引用您创建的 XML 布局。这就是为什么最后的 3 行代码失败的原因,因为 TextView
正在尝试设置其 LayoutParams
以决定其父级应该如何放置和测量它,但是在这种情况下它没有父级。我建议做的是给 XML 中的 RelativeLayout
一个 id
属性,以便在 Activity
代码中获得对它的引用,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="home_screen_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"/>
然后在您的 Activity
代码中,调整它以便您使用 XML 文件的资源 ID 进行调用。如果我们假设它在您的资源目录的布局文件夹中称为 act_main.xml
(即在 src/main/resources/layout/act_main.xml
中),您将调用 setContentView(R.layout.act_main)
作为 onCreate()
中 [=] 之后的第一行23=] 调用,以便框架有机会解析您的 XML 并对其进行膨胀(即实例化,计算大小和
确定其组件的放置等)。之后,使用 findViewById(R.id.home_screen_layout)
获取对 RelativeLayout
的引用,以便您可以创建一个新的 TextView
并将其添加到您已经膨胀的布局中。
package com.example.android.testerapp1;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// make your view components private members as findViewById calls are expensive for the framework
private RelativeLayout homeScreenLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Have the activity inflate the XML file with your RelativeLayout
setContentView(R.layout.act_main);
// Now that it is inflated, get a reference to that parent
homeScreenLayout = (RelativeLayout) findViewById(R.id.home_screen_layout);
// Dynamically create a TextView associated with this Activity's context
TextView homeScreen = new TextView(this);
homeScreen.setText("Welcome to Test App 001" + "\nThis TextView was created dynamically in Java!");
homeScreen.setTextSize(24);
homeScreen.setTextColor(Color.CYAN);
homeScreen.setCursorVisible(true);
homeScreen.setPadding(16,56,16,56);
homeScreen.setBackgroundColor(Color.BLACK);
homeScreen.setGravity(Gravity.CENTER);
//dynamically set width to dp (converted to pixels ~600) and height to 'wrap content'
// convert dp amount to pixels for size
final float scale = getResources().getDisplayMetrics().density;
int pixelWidth = (int) (2000 / scale + 0.5f);
// Adjust the placement in the parent
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(pixelWidth , RelativeLayout.LayoutParams.WRAP_CONTENT)
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); // make sure to use the function which takes a boolean value for rules like CENTER_IN_PARENT
homeScreen.setLayoutParams(params); // Add these parameters to the textview
// Let the layout know about your newly created textview so that it can re-draw its canvas
homeScreenLayout.addView(homeScreen);
}
}
请注意,我要补充一点,您可以在 XML 中相对轻松地完成您正在做的事情,但由于您询问了具体的编程设置,我不会深入探讨这方面的细节。但如果您对某些结构化资源感兴趣,我建议您查看 Android Developer Guide, specifically the section on XML layouts and how they interact with Activities
编辑: 请注意我对 Activity 代码所做的更改。主要部分是首先用 setContentView(int id)
膨胀空的 RelativeLayout xml,然后将另一个 TextView
添加到给定的布局。我提供的有关 CENTER_IN_PARENT
行的代码存在一个小错误。根据 [docs](https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#addRule(int, int)),添加使用布尔值的规则时必须使用函数的 addRule(int, int)
版本。