0

DetailsFragment クラスがあり、このクラスは「fragment」xml に 2 つのボタンを追加しますが、「scroller」に 2 つのボタンを追加すると、「ScrollView は直接の子を 1 つしかホストできません」と表示されます。Javaコードで「スクローラー」に2つのボタンを追加するのを手伝ってください。

public class DetailsFragment extends Fragment {

      @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            if (container == null) {
                return null;
            }
     LinearLayout linearLayout = (LinearLayout)findViewById(R.id.t);
            ScrollView scroller = new ScrollView(getActivity());
            Button m =new  Button(getActivity());
            m.setText("adfgadgfdsfg");
            m.setWidth(100);
            m.setHeight(30);
            m.setTextSize(30);
           scroller.addView(m);

     //*** expiation in this code but when i clear m1 code is work   
            Button m1 =new  Button(getActivity());
            m1.setText("adfgadgfdsfg");
            m1.setWidth(100);
            m1.setHeight(30);
            m1.setTextSize(30);
           scroller.addView(m1);


        }
    }

コード xml レイアウト

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ti"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <fragment 
            android:id="@+id/titles"
            android:layout_width="match_parent" 
            android:layout_height="match_parent"
           android:background="@color/color_detial_fragment" 

             />
           <LinearLayout 
           android:id="@+id/t"
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
          ></LinearLayout>
</FrameLayout>
4

2 に答える 2

0

エラー状態として:

ScrollView can host only one direct child

したがって、レイアウト ( LinearLayout/ RelativeLayout) を 内に作成し、ScrollViewこのレイアウト内にボタンを配置する必要があります。このような:

 LinearLayout ll = (LinearLayout)findViewById(R.id.yourLinearLayout);
 Button m =new  Button(getActivity());
 m.setText("adfgadgfdsfg");
 m.setWidth(100);
 m.setHeight(30);
 m.setTextSize(30);
 ll.addView(m); 

 Button m1 =new  Button(getActivity());
 m1.setText("adfgadgfdsfg");
 m1.setWidth(100);
 m1.setHeight(30);
 m1.setTextSize(30);
 ll.addView(m1);

 scroller.addView(ll);
于 2013-05-16T12:55:58.017 に答える
0

scrollview に 2 つの子を追加することはできません。許可される子は 1 つだけです。線形レイアウトを 1 つ作成し、そのレイアウトにボタンを追加してから、このレイアウトをスクロール ビューに追加します。

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.layout1);
Button btn = new Button(this); 
btn.setText("MyButton"); 
linearLayout.addView(btn); 

Button btn2 = new Button(this); 
btn2.setText("MyButton"); 
linearLayout.addView(btn2); 

scrollview.addView(linearLayout);
于 2013-05-16T12:56:09.327 に答える