我正在尝试在我的第一个活动的onCreate()方法中创建一个horizontalscrollview,因为我想要滚动大量的textview。以下是我到目前为止的情况:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
LinearLayout linscrollview;
HorizontalScrollView scrollview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollview = (HorizontalScrollView) findViewById(R.id.scrollview_layout);
linscrollview = new LinearLayout(this);
for(int i=0; i<5; i++) {
TextView tv = new TextView(this);
tv.setWidth(LayoutParams.WRAP_CONTENT);
tv.setHeight(LayoutParams.WRAP_CONTENT);
tv.setText("" + i);
tv.setTextSize(20);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
params.setMargins(10, 0, 10, 0);
tv.setLayoutParams(params);
tv.setId(i);
linscrollview.addView(tv);
}
scrollview.addView(linscrollview);
}
@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;
}
}
我没有收到任何错误,但没有显示任何文字视图。
答案 0 :(得分:2)
您的问题可能与setWidth和setHeight方法有关。它们设置了TextView宽度和高度的精确值,如文档中所述:
使TextView正好宽这么多像素。你可以做到 同样的事情是在LayoutParams中指定这个数字。
http://developer.android.com/reference/android/widget/TextView.html#setWidth(int)
您要做的是为TextView设置LayoutParams,因为您已经在代码中稍微向下。所以只需要摆脱这两个方法调用就可以了。
答案 1 :(得分:1)
这是为textview实现水平滚动的一大块代码,根据要求修改相同的内容。
textView.setHorizontallyScrolling(true);
textView.setSingleLine(true);
textView.setMovementMethod(new ScrollingMovementMethod());
textView.setHorizontalScrollBarEnabled(true);
textView.setSelected(true);
答案 2 :(得分:0)
请这样做
linscrollview .setOrientation(LinearLayout.HORIZONTAL);
和ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(FILL_PARENT, WRAP_CONTENT);
答案 3 :(得分:0)
我在这里编码。它会给你像horizonatal listview的列表
String[] name={"PRASHANT","PRASHANT","PRASHANT","PRASHANT","PRASHANT","PRASHANT","PRASHANT"} ;
myLInearLayoutmain =(LinearLayout) findViewById(R.id.linearLayoutmain);
for(int i =0;i<6;i++)
{
LinearLayout li=new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
li.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams paramsnew = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
params1.setMargins(30, 20, 30, 0);
//add textView
valueTV = new TextView(this);
valueTV.setText(""+name[i]);
valueTV.setId(5);
valueTV.setLayoutParams(paramsnew);
valueTV.setGravity(Gravity.CENTER);
// adding Button to linear
valueB = new Button(this);
valueB.setText(""+name[i]);
valueB.setId(i);
valueB.setLayoutParams(params);
valueB.setOnClickListener(this);
valueB.setGravity(Gravity.CENTER);
//add the textView and the Button to LinearLayout
li.addView(valueTV);
li.addView(valueB);
li.addView(img);
li.setLayoutParams(params1);
myLInearLayoutmain.addView(li);
}