的setContentView和布局(机器人) [英] setContentView and layout (android)
问题描述
我有一个活动的布局,但我也希望显示一些文本。
我不能使用的标签,因为我没有这两个布局。是否有任何等效,可以使布局和的setContentView(TextView的)语句一起工作?
我的code是:
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
意向意图= getIntent();
字符串消息= intent.getStringExtra(FirstActivity.EXTRA_MESSAGE); TextView的文本=新的TextView(本);
text.setText(消息);
的setContentView(R.layout.second);
}
我试过
RelativeLayout的RelativeLayout的=新的RelativeLayout(本);
RelativeLayout.LayoutParams lParams =新RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
然后
relativeLayout.addView(文本);
但我得到这个错误:
字段
ViewGroup.LayoutParams.FILL_PARENT
是pcated德$ P $
块引用>解决方案您与方法有点糊涂
的setContentView()
。您可以设置从活动的布局
键,进入这个布局
您可以添加TextViews
只要你想或许多需要。<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:背景=#3B3B3B
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直
机器人:填充=10dp> <的TextView
机器人:ID =@ + ID /标题
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/标题
机器人:TEXTSIZE =26dp
机器人:layout_marginTop =10dp
机器人:文字样式=大胆
/> <的TextView
机器人:ID =@ +帐号/电邮
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/邮件
机器人:layout_marginTop =15dp
机器人:layout_marginBottom =5DP
/> <的EditText
机器人:ID =@ + ID / emailForm
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
安卓的inputType =textEmailAddress
机器人:背景=@绘制/ MyShape的
/> <的TextView
机器人:ID =@ + ID /密码
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/密码
机器人:layout_marginTop =10dp
机器人:layout_marginBottom =5DP
/> <的EditText
机器人:ID =@ + ID / passwordForm
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
安卓的inputType =textPassword
机器人:背景=@绘制/ MyShape的
/> <按钮
机器人:ID =@ + ID / loginBtn
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/ loginBtn
机器人:layout_marginTop =40dp
/> <的TextView
机器人:ID =@ + ID / registerLink
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:重力=CENTER_HORIZONTAL
机器人:文字样式=大胆
机器人:文字=@字符串/注册
机器人:文字颜色=#00BADB
机器人:layout_marginTop =20dp
/> <的TextView
机器人:ID =@ + ID / registerLink
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:重力=CENTER_HORIZONTAL
机器人:文字样式=大胆
机器人:文字=@字符串/注册
机器人:文字颜色=#00BADB
机器人:layout_marginTop =20dp
/> <的TextView
机器人:ID =@ + ID / registerLink
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:重力=CENTER_HORIZONTAL
机器人:文字样式=大胆
机器人:文字=@字符串/注册
机器人:文字颜色=#00BADB
机器人:layout_marginTop =20dp
/>
< / LinearLayout中>或者你也可以设置programmaticaly布局和添加也添加小部件在这种方式。
RelativeLayout的RelativeLayout的=新的RelativeLayout(本);RelativeLayout.LayoutParams lParams =新RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);//创建部件
TextView的文本=新的TextView(本);
text.setText(简单的虚拟文本); //最后你的TextView添加到RelativeLayout的
relativeLayout.addView(文本); //并设置您的布局像主要内容
的setContentView(RelativeLayout的,lParams);您也可以为小部件对准设置的规则。
I have a layout for an activity but I also want to display some text. I can't use the tag because I don't have two layouts. Is there any equivalent that can make a layout and a setContentView(textView) statement work together?
my code is:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); String message = intent.getStringExtra(FirstActivity.EXTRA_MESSAGE); TextView text = new TextView(this); text.setText(message); setContentView(R.layout.second); }
I tried
RelativeLayout relativeLayout = new RelativeLayout(this); RelativeLayout.LayoutParams lParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
and then
relativeLayout.addView(text);
but I got this error:
The field
ViewGroup.LayoutParams.FILL_PARENT
is deprecated
解决方案You are little confused with method
setContentView()
. You could set layout from yourActivity
and into thisLayout
you can addTextViews
as many as you want or need.<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#3B3B3B" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/title" android:textSize="26dp" android:layout_marginTop="10dp" android:textStyle="bold" /> <TextView android:id="@+id/email" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/email" android:layout_marginTop="15dp" android:layout_marginBottom="5dp" /> <EditText android:id="@+id/emailForm" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textEmailAddress" android:background="@drawable/myshape" /> <TextView android:id="@+id/password" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/password" android:layout_marginTop="10dp" android:layout_marginBottom="5dp" /> <EditText android:id="@+id/passwordForm" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:background="@drawable/myshape" /> <Button android:id="@+id/loginBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/loginBtn" android:layout_marginTop="40dp" /> <TextView android:id="@+id/registerLink" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textStyle="bold" android:text="@string/register" android:textColor="#00BADB" android:layout_marginTop="20dp" /> <TextView android:id="@+id/registerLink" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textStyle="bold" android:text="@string/register" android:textColor="#00BADB" android:layout_marginTop="20dp" /> <TextView android:id="@+id/registerLink" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textStyle="bold" android:text="@string/register" android:textColor="#00BADB" android:layout_marginTop="20dp" /> </LinearLayout>
Or you can set layout programmaticaly and add also add widgets in this approach.
RelativeLayout relativeLayout = new RelativeLayout(this); RelativeLayout.LayoutParams lParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT); //create widgets TextView text = new TextView(this); text.setText("Simply dummy text"); // finally add your TextView to the RelativeLayout relativeLayout.addView(text); // and set your layout like main content setContentView(relativeLayout, lParams);
You can also set rules for aligning of widgets.
这篇关于的setContentView和布局(机器人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!