为什么我的TextView不会添加到RelativeLayout?

时间:2016-07-03 18:07:03

标签: java android android-layout

我正在尝试以编程方式将我的textView添加到我的应用程序中,但是当我运行它时它不会出现。我在XML中放置一个按钮来测试relativeLayout并且看起来很好,但textView没有&#39因此,我要放下代码?我的代码如下。提前谢谢!

       @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>

2 个答案:

答案 0 :(得分:2)

下面的代码对我有用, 在您的活动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对象中的子视图。

答案 1 :(得分:0)

试试这个。

     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();