gpt4 book ai didi

android - ScrollView 不工作它只显示空 View

转载 作者:行者123 更新时间:2023-11-29 17:51:14 25 4
gpt4 key购买 nike

我正在尝试在 ScrollView 中添加一个 TextView ,然后以编程方式将此 ScrollView 添加到相对布局,但它只显示空 View ,即使仅将 TextView 添加到相对布局也是如此。

    TextView aboutTextView = new TextView(this);
aboutTextView.setText(R.string.aboutText);

aboutTextView.setId(5);
aboutTextView.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

ScrollView scroll = new ScrollView(this);
scroll.setPadding(R.dimen.scroll_padding, R.dimen.scroll_padding,
R.dimen.scroll_padding, R.dimen.scroll_padding);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
scroll.addView(aboutTextView);

RelativeLayout fragmentContainer = (RelativeLayout) findViewById(R.id.fragmentContainer);
fragmentContainer.addView(scroll);

最佳答案

试试这段代码。我希望它有用。

1) Java代码

package com.temp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView aboutTextView = new TextView(this);
aboutTextView
.setText("Temp Text Temp Text Temp");

aboutTextView.setId(5);
aboutTextView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

ScrollView scroll = new ScrollView(this);
scroll.setPadding(5, 5, 5, 5);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
scroll.addView(aboutTextView);

RelativeLayout fragmentContainer = (RelativeLayout) findViewById(R.id.fragmentContainer);
fragmentContainer.addView(scroll);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

2) XML代码

<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
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</RelativeLayout>

</RelativeLayout>

Place of Temp Text Temp Text Temp 放置您的文本。
它对我有用。检查一下。

谢谢。

关于android - ScrollView 不工作它只显示空 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22684068/

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