[英]Why won't my TextView add to RelativeLayout?
我正在尝试以编程方式将textView添加到我的应用程序中,但是在运行它时却没有出现。我在XML中放置了一个按钮来测试relativeLayout,它看起来还不错,但是textView没有,所以我把下降到代码? 我的代码如下。 提前致谢!
@Override
public void onResponse(String response) {
StringX = response;
Log.e("RESPONSE", response);
TextView valueTV = new TextView(getBaseContext());
valueTV.setText("Hello!");
valueTV.setTextSize(20);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
valueTV.setLayoutParams(params);
valueTV.setTextColor(Color.argb(1, 0, 95, 0));
valueTV.setId(0);
scrollerF.addView(valueTV);
}
而我的布局:
<?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="com.example.garethpower92.tourlink_ireland.coupons">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="70dp"
android:background="#FFFFFF"
android:id="@+id/scrollerMain"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:fillViewport="false">
<RelativeLayout
android:id="@+id/rlX"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</RelativeLayout>
</ScrollView>
<TextView
android:layout_width="fill_parent"
android:layout_height="70dp"
android:text="Discounts"
android:id="@+id/textView"
android:textColor="#FFFFFF"
android:textSize="40sp"
android:background="#009500"
android:textAlignment="center"
android:paddingTop="10sp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
下面的代码对我有用,在您的活动onCreate方法中添加以下代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView mTextv = new TextView(getApplicationContext());
mTextv.setText("This is Text....!!");
mTextv.setGravity(Gravity.CENTER_VERTICAL);
linearLayout = (LinearLayout) findViewById(R.id.lLayout);
mTextv.setTextSize(20);
mTextv.setTypeface(Typeface.MONOSPACE);
mTextv.setTextColor(Color.parseColor("#454045"));
linearLayout.addView(mTextv);
}
线性布局是您将动态添加的TextView的父布局,因此广告textview作为linearLayout对象中的子视图。
尝试这个。
TextView valueTV = new TextView(scrollerF.getContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
valueTV.setLayoutParams(params.addRule(RelativeLayout.CENTER_IN_PARENT));
valueTV.setText("Hello!");
valueTV.setTextSize(20);
scrollerF.addView(valueTV);
scrollerF.invalidate();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.