[英]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.