我正在尝试在Scroll视图中添加Text-view,然后以编程方式将此滚动视图添加到Relative-layout,但它只显示空视图,甚至只将文本视图添加到Relative-layout其工作。
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);
答案 0 :(得分:1)
ScrollView scroll = new ScrollView(context);
scroll.setBackgroundColor(android.R.color.transparent);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
scroll.addView(yourview);
答案 1 :(得分:1)
试试这段代码。我希望它会有用。
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>
Temp Text Temp Text Temp
的位置放置了您的文字。 谢谢。
答案 2 :(得分:1)
在抓取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);
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);