我正在学习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
设置宽度和高度吗?
答案 0 :(得分:0)
您可以在构造函数上设置宽度和高度,然后使用它
Relative.LayoutParams(int width, int height)
所以你需要这样做:
homeScreen.setLayoutParams(width , height);
答案 1 :(得分:0)
setContentView()
调用应该用于设置全屏的布局。您当前在Activity代码中所做的只是将TextView设置为屏幕的完整视图,因此Activity没有引用您创建的XML布局。这就是为什么最后3行代码失败的原因,因为TextView
正试图设置其LayoutParams
的父级应如何放置和测量它,但是在这种情况下它没有父级。我建议做的是为XML中的id
提供RelativeLayout
属性,以便在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()
调用之后,框架有机会解析您的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,特别是关于XML layouts and how they interact with Activities的部分
编辑:请注意我对活动代码所做的更改。主要部分首先使用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)
版本。