2017-08-29 113 views
0
public class Dues extends Activity { 

    static final String[] alphabets = new String[] { 
      "A", "B", "C", "D"}; 

    static final String[] alphabets1 = new String[] { 
      "E", "F", "G", "H"}; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_dues); 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_1, alphabets); 

     ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_1, alphabets1); 

     RelativeLayout relativeLayout = new RelativeLayout(this); 

     RelativeLayout.LayoutParams relativeLayoutParams = new 
     RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.FILL_PARENT, 
            RelativeLayout.LayoutParams.FILL_PARENT); 

     GridView gridView= new GridView(this); 

     gridView.setLayoutParams(new 
         GridView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
     gridView.setNumColumns(4); 

     gridView.setAdapter(adapter); 

     Drawable myIcon = getResources().getDrawable(R.drawable.bg); 

     gridView.setBackground(myIcon); 

     GridView gridView1 = new GridView(this); 

     gridView1.setLayoutParams(new 
         GridView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

     gridView1.setNumColumns(4); 

     gridView1.setAdapter(adapter); 


     Drawable myIcon = getResources().getDrawable(R.drawable.bg); 

     gridView1.setBackground(myIcon); 

    } 
}  

因此,此代码创建2个gridview,但其重叠。请帮帮我。 ABCD和EFGH都相互重叠。 这样我想添加多个GridView使用滚动查看。 任何人都可以帮助我这个请。 我希望它是可见的一个在另一个之下。如何以编程方式创建多个gridview

gridview的数量是动态的,也没有固定的数量,因此我希望它以编程方式创建,而不是通过xml文件。 也我希望它是绿色透明,所以任何人都可以帮助我也

回答

1

A GridView是滚动的,因此你不能达到你想要的东西与常规ScrollView。另一个问题是您正在创建两个GridView,但您并未将它们添加到任何父级。您需要做的是创建一个NestedScrollView,其中LinearLayoutorientation=vertical并将其添加到GridView。将match_parent应用于NestedScrollViewLinearLayout的高度和宽度。另请注意,请勿使用LayoutParams.FILL_PARENT作为GridView高度,请使用WRAP_CONTENT

+0

噢好吧!,非常感谢。我做了wrap_content,但是然后gridview不可见 –

+0

你能帮助它的代码吗? –

+0

按照我说的一步一步来做。你不能使用ScrollView来实现这一点,你必须使用NestedScrollView – Ricardo

相关问题