Android ScrollView不工作,只显示空视图
我试图在一个滚动视图中添加一个文本视图,然后以编程方式将这个滚动视图添加到相对布局中,但它只是显示空视图,甚至只将文本视图添加到相对布局中Android ScrollView不工作,只显示空视图,android,android-relativelayout,textview,android-scrollview,Android,Android Relativelayout,Textview,Android Scrollview,我试图在一个滚动视图中添加一个文本视图,然后以编程方式将这个滚动视图添加到相对布局中,但它只是显示空视图,甚至只将文本视图添加到相对布局中 TextView aboutTextView = new TextView(this); aboutTextView.setText(R.string.aboutText); aboutTextView.setId(5); aboutTextView.setLayoutParams(new LayoutParams(
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代码 2) XML代码
放置Temp Text Temp Text Temp
放置文本它对我有用。过来看。
谢谢。抓取RelativeLayout对象时,不要在同一语句中添加ScrollView。 我认为您应该指定正在使用的LayoutParams的哪个实现
aboutTextView.setLayoutParams(new ScrollView.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
ScrollView scroll = new ScrollView(this);
int scrollPadding = getResources().getDimensionPixelSize(R.dimen.scroll_padding);
scroll.setPadding(scrollPadding, scrollPadding, scrollPadding, scrollPadding);
scroll.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
scroll.addView(aboutTextView);
RelativeLayout fragmentContainer = (RelativeLayout) findViewById(R.id.fragmentContainer);
fragmentContainer;
fragmentContainer.addView(scroll);
也看到了这一点,感谢您的快速回复,我在线性布局中添加了textview,然后将其添加到scrollview中。scrollview也设置了scroll view的背景色,但它仍然不工作。您需要可滚动的textview吗?@runtime还是想要xml格式?android:maxLines=“AN_INTEGER”android:scrollbars=“垂直”然后使用:textView.setMovementMethod(新的ScrollingMovementMethod());是的,它的工作很好,这是因为我设置它的方式存在填充问题。如果要在dimens.xml中定义它,必须获取填充值getDimensionPixelSize。如下所示:getResources().getDimensionPixelSize(R.dimen.scroll\u padding);
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;
}
}
<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>
aboutTextView.setLayoutParams(new ScrollView.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
ScrollView scroll = new ScrollView(this);
int scrollPadding = getResources().getDimensionPixelSize(R.dimen.scroll_padding);
scroll.setPadding(scrollPadding, scrollPadding, scrollPadding, scrollPadding);
scroll.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
scroll.addView(aboutTextView);
RelativeLayout fragmentContainer = (RelativeLayout) findViewById(R.id.fragmentContainer);
fragmentContainer;
fragmentContainer.addView(scroll);