gpt4 book ai didi

java - 为什么我的 TextView 不添加到relativelayout?

转载 作者:行者123 更新时间:2023-12-02 03:30:25 29 4
gpt4 key购买 nike

我正在尝试以编程方式将我的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>

最佳答案

下面的代码对我有用,在您的 Activity 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 对象中的 subview 。

关于java - 为什么我的 TextView 不添加到relativelayout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38172869/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com