[英]Programmatically setting TextView to RelativeLayout.CENTER_OF_PARENT
我只是在学习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);
}
}
我现在已经看过大约10次此类帖子,而且它们都具有我似乎无法正确实现的相同解决方案,这可能是我代码的另一部分? 可能在哪里我也使用setLayoutParams
设置宽度和高度?
您可以在构造函数上设置宽度和高度,然后使用它
Relative.LayoutParams(int width, int height)
所以你需要这样做:
homeScreen.setLayoutParams(width , height);
应该使用setContentView()
调用来设置全屏布局。 当前在“活动”代码中正在执行的操作只是将TextView设置为屏幕的完整视图,因此“活动”没有对您创建的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()
在super()
调用之后super()
以便框架有机会解析您的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开发人员指南》 ,特别是有关XML布局及其与活动的交互方式的部分。
编辑:请注意我对活动代码的更改。 主要部分是首先用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)
版本添加使用布尔值的规则时的函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.