简体   繁体   English

Android ScrollView似乎无法识别其直接子级

[英]Android ScrollView seems doesn't recognize its direct child

I'm using a ScrollView to show all the products that users add to his shopping cart. 我正在使用ScrollView来显示用户添加到他的购物车中的所有产品。 I create a scroll view and, into it, i create a linear layout 'intern'. 我创建一个滚动视图,并在其中创建一个线性布局“ intern”。 Then, i build every single row in loop. 然后,我建立循环中的每一行。 Here's the XML 这是XML

<ScrollView
    android:id="@+id/scroll"
    android:layout_width="300dp"
    android:layout_height="230dp"
    android:layout_marginLeft="10dp" >

    <LinearLayout 
        android:id="@+id/intern"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></LinearLayout>
</ScrollView>

Here's the Java code. 这是Java代码。

for(Carrello cns : carrello){

            LinearLayout prodotto = new LinearLayout(context);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(/**/LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            prodotto.setLayoutParams(lp);
            //Product name
            TextView nome_p = new TextView(context);
            nome_p.setWidth(dpToPx(115));
            nome_p.setTextColor(getResources().getColor(R.color.grey));
            nome_p.setText(cns.getNomeProdotto());  /**/
            //Counts
            TextView quantita = new TextView(context);
            quantita.setWidth(dpToPx(80));
            quantita.setGravity(Gravity.CENTER);
            quantita.setTextColor(getResources().getColor(R.color.grey));
            quantita.setTextAppearance(context, R.style.quantita);
            quantita.setText(cns.getQuantita());  
            //Price
            TextView prezzo = new TextView(context);
            prezzo.setWidth(dpToPx(70));
            prezzo.setPadding(dpToPx(5), 0, 0, 0);
            prezzo.setTextColor(getResources().getColor(R.color.grey));
            prezzo.setTextAppearance(context, R.style.prezzo);
            prezzo.setText("€"+cns.getCosto());
            //Cancella
            Button canc = new Button(context);
            LinearLayout.LayoutParams lp_btn = new LinearLayout.LayoutParams(/*LinearLayout.LayoutParams.WRAP_CONTENT*/40,40/*LinearLayout.LayoutParams.WRAP_CONTENT*/);
            lp_btn.setMargins(0, dpToPx(10), 0, 0);
            canc.setLayoutParams(lp_btn);
            canc.setBackground(getResources().getDrawable(R.drawable.btn_canc));
            canc.setId(Integer.parseInt(cns.getIDprodotto()));/**/
            Log.d("ID-CANC",String.valueOf(canc.getId()));
            //create the line
            prodotto.addView(nome_p);
            prodotto.addView(quantita);
            prodotto.addView(prezzo);
            prodotto.addView(canc);
            intern.addView(prodotto);
            i++;
}//end for

Then, add intern to the scroll view: 然后,将intern添加到滚动视图:

scroll.addView(intern);

But when i run the emulator, logCat says that "ScrollView can host only one direct child", and the app crashes. 但是,当我运行模拟器时,logCat会说“ ScrollView只能容纳一个直子”,并且该应用程序崩溃。

Don't use scroll.addView(intern); 不要使用scroll.addView(intern); ,

because you have already defined child in SCrollView in your layout.xml 因为您已经在layout.xml中的SCrollView中定义了子级

Instead try as below. 而是尝试如下。

LinearLayout intern = (LinearLayout) findViewById(R.id.intern);

then use your for loop to add views in intern 然后使用for循环在intern添加视图

That,s it... 而已...

No need to call addview for scroll 无需调用addview进行滚动

Hope this helps 希望这可以帮助

The ScrollView already has a child defined in the layout file. ScrollView已经在布局文件中定义了一个子级。 scroll.addView(intern); will cause error. 会导致错误。

Just add scroll=(ScrollView) findViewById(R.id.intern) before the loop and remove scroll.addView(intern); 只需在循环之前添加scroll=(ScrollView) findViewById(R.id.intern)并删除scroll.addView(intern); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM