2013-05-30 36 views
-1

我想添加到我的应用程序ScreenView,但我只是得到错误。Android如何添加ScrollView

我的布局:

<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:background="@color/bg" 
    android:id="@+id/mainlayout" 
    tools:context=".NwActivity" android:baselineAligned="false"> 

    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:id="@+id/rlA" > 

      <TextView 
       android:id="@+id/teamA" 
       android:text="@string/teamA" 
       android:textSize="24sp" 
       style="@style/Txt" /> 

      <Button 
       android:id="@+id/pAddA" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/teamA" 
       style="@style/Txt" 
       android:text="@string/pAdd" 
       android:onClick="addA" /> 

      <EditText 
       android:id="@+id/eta0" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/pAddA" 
       style="@style/Txt" 
       android:hint="@string/pHint" /> 

     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:id="@+id/rlB" > 

      <TextView 
       android:id="@+id/teamB" 
       android:text="@string/teamB" 
       android:textSize="24sp" 
       style="@style/Txt" /> 

      <Button 
       android:id="@+id/pAddB" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/teamB" 
       style="@style/Txt" 
       android:text="@string/pAdd" 
       android:onClick="addB" /> 

     </RelativeLayout> 

    </ScrollView> 

</LinearLayout> 

活动:

package some.program; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.EditText; 
import android.widget.RelativeLayout; 

public class NwActivity extends Activity { 

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

    public int skid = 0; 

    public void addA(View view) { 
     if(skid < 14) { 
      skid = skid + 1; 
      //LinearLayout ll = (LinearLayout) findViewById(R.id.mainlayout); 
      RelativeLayout rl = (RelativeLayout) findViewById(R.id.rlA); 

      EditText et1 = new EditText(this); 
      et1.setHint(R.string.pHint); 
      String strID = "eta" + skid; 
      int nid = getResources().getIdentifier(strID, "id", this.getPackageName()); 
      et1.setId(nid); 

      int oskid = skid - 1; 

      int oid; 


      String resID = "eta" + oskid; 
      oid = getResources().getIdentifier(resID, "id", this.getPackageName()); 


      RelativeLayout.LayoutParams params1= new RelativeLayout.LayoutParams 
         (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

      params1.addRule(RelativeLayout.BELOW, oid); 
      params1.addRule(RelativeLayout.CENTER_HORIZONTAL); 
      et1.setLayoutParams(params1); 
      et1.setTextAppearance(this, R.style.Txt); 
      rl.addView(et1); 
     } 
    } 

    public void addB(View view) { 

    } 

} 

我如何添加滚动型,即不会让乱我的布局?点击按钮时,应用程序正在创建EditText表单。一切工作正常,但所有EditText字段不适合布局,不会出现。

+0

你得到了什么错误?请显示logCat。 –

+0

滚动视图不能超过1个孩子 – njzk2

回答

0

有可能是在布局其他一些问题,一些问题我已经找到的

首先ScrollView只需要一个直接的孩子,而你是在ScrollView声明两个观点R1a和R1b。 (你可以把它通过把ScrollView作为主布局,并把LinearLayout代替ScrollView工作)

其次,你应该为你的布局添加layout_widthlayout_height属性(我猜你是在你的STYLES已经宣布)。

+0

是的,我声明宽度和高度Txt风格。当我替换ScrollView和LinearLayout时工作得很好。是否有可能,当我点击按钮应用程序向下滚动? (对不起,我的英语不好) – user2350530

相关问题