【问题标题】:Programmatically creating a horizontalscrollview not working以编程方式创建水平滚动视图不起作用
【发布时间】:2014-01-14 04:05:42
【问题描述】:

我试图在我的第一个活动的 onCreate() 方法中创建一个水平滚动视图,因为我想制作大量的文本视图来滚动浏览。这是我目前所拥有的:

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;
    }

}

我没有收到任何错误,但是没有显示任何文本视图。

【问题讨论】:

    标签: android android-view android-scrollview


    【解决方案1】:

    您的问题可能与 setWidth 和 setHeight 方法有关。他们按照文档中的描述设置了 TextView 宽度和高度的精确值(以像素为单位):

    使 TextView 正好有这么多像素宽。你可以这样做 通过在 LayoutParams 中指定这个数字来实现同样的效果。

    http://developer.android.com/reference/android/widget/TextView.html#setWidth(int)

    您想要做的是为 TextView 设置 LayoutParams,因为您已经在代码中稍微走得更远了。所以只要摆脱这两个方法调用,它应该可以工作。

    【讨论】:

    • @ThelT 修复了它!谢谢!
    【解决方案2】:

    这是一段实现textview水平滚动的代码,根据需求修改。

                        textView.setHorizontallyScrolling(true);
                        textView.setSingleLine(true);
                        textView.setMovementMethod(new ScrollingMovementMethod());
                        textView.setHorizontalScrollBarEnabled(true);
                        textView.setSelected(true);
    

    【讨论】:

      【解决方案3】:

      请这样做

      linscrollview .setOrientation(LinearLayout.HORIZONTAL); 
      

      ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(FILL_PARENT, WRAP_CONTENT);

      【讨论】:

        【解决方案4】:

        我的编码在这里。它会给你像水平列表视图这样的列表

        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);
        }
        

        【讨论】:

          猜你喜欢
          • 2021-04-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-06
          相关资源
          最近更新 更多