Horizo​​ntalScrollView不显示内容 - 这必须简单。我错过了什么?

时间:2014-03-09 08:06:15

标签: android android-layout

我的代码包含一个带有LinearLayout子元素的简单Horizo​​ntalScrollView。我想以编程方式将TextViews添加到LinearLayout。代码执行时没有错误,但没有显示任何TextView。净网 - 我的三星s3上有一个空白的活动屏幕。我可能做错了什么?任何和所有的帮助非常感谢。非常感谢提前。

Horizo​​ntalScrollActivity.java

public class HorizontalScrollActivity extends Activity {
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.horizontal_scroll, menu);
    return true;
}
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_horizontal_scroll);
    LayoutInflater inflater=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v=inflater.inflate(R.layout.activity_horizontal_scroll, null);
    HorizontalScrollView ribbon=(HorizontalScrollView)v.findViewById(R.id.ribbon);
    LinearLayout ribbonHolder=(LinearLayout)ribbon.findViewById(R.id.ribbon_holder);
    for (int i=0;i<10;i++) {
        TextView ribbonItem=new TextView(this);
        ribbonItem.setText("text - "+i);
        ribbonItem.setWidth(100);
        ribbonItem.setLayoutParams(new RelativeLayout.LayoutParams((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT));
        ribbonHolder.addView(ribbonItem);
    }
}

}

对应的布局xml文件(activity_horizo​​ntal_scroll.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".HorizontalScrollActivity" >
<HorizontalScrollView 
    android:id="@+id/ribbon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/ribbon_holder"
        android:orientation="horizontal"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"/>
</HorizontalScrollView>
</LinearLayout>

1 个答案:

答案 0 :(得分:2)

为什么你再次虚张声势activity_horizontal_scroll

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_horizontal_scroll);

HorizontalScrollView ribbon=(HorizontalScrollView).findViewById(R.id.ribbon);
LinearLayout ribbonHolder=(LinearLayout)findViewById(R.id.ribbon_holder);
for (int i=0;i<10;i++) {
    TextView ribbonItem=new TextView(this);
    ribbonItem.setText("text - "+i);
    ribbonItem.setWidth(100);
    ribbonItem.setLayoutParams(new RelativeLayout.LayoutParams((int)LayoutParams.WRAP_CONTENT,(int)LayoutParams.WRAP_CONTENT));
    ribbonHolder.addView(ribbonItem);
}
ribbon.addView(ribbonHolder);
}

尝试这种方式并就此提供反馈。