在ScrollView中添加多个视图?

时间:2014-03-15 16:13:53

标签: android android-layout android-linearlayout android-scrollview

我正在使用Android应用。我有一些问题..

这是我的应用: enter image description here

粉红色矩形是ScrollView。我想将TextView添加到相同的ScrollView。我知道我需要一个LinearLayout并在此布局中添加我的视图。我需要将LinearLayout添加到我的ScrollView之后。这是我的ScrollView类,我的方法setClassicLabel用于实现TextViewsetClassicButton以在我的ScrollView中实现Button

这是我的ScrollView.java

public  class MyScrollView extends ScrollView {

private JSONParser  jParser;
private JSONObject  contenuScrollView;
private Context     context;

@SuppressLint("NewApi")
public MyScrollView(Context context, JSONArray listScrollView) throws JSONException {
    super(context);

    this.context = context;
}

public void onCreate(Bundle savedInstanceState) throws JSONException {
}

/* Création de TextView */
public void setClassicLabel(JSONArray listLabel, LinearLayout ll) throws JSONException {
        if (listLabel.length() > 0)
            {
            DisplayMetrics metrics = new DisplayMetrics();
            ll = new LinearLayout(getContext());
            ll.setOrientation(LinearLayout.VERTICAL);
            LinearLayout.LayoutParams paramsll = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            for (int i = 0; i < listLabel.length(); i++) {

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) (getContext().getResources().getDisplayMetrics().widthPixels * listLabel.getJSONObject(i).getDouble("size_x")),
                                                                                (int) (getContext().getResources().getDisplayMetrics().heightPixels * listLabel.getJSONObject(i).getDouble("size_y")));
                params.leftMargin = (int) (getContext().getResources().getDisplayMetrics().widthPixels * listLabel.getJSONObject(i).getDouble("position_x"));
                params.topMargin = (int) (getContext().getResources().getDisplayMetrics().heightPixels * listLabel.getJSONObject(i).getDouble("position_y"));
                TextView tv = new TextView(context);
                tv.setText(listLabel.getJSONObject(i).getString("text"));
                tv.setTextSize(listLabel.getJSONObject(i).getInt("police_size"));
                tv.setTextColor(Color.parseColor(listLabel.getJSONObject(i).getString("colortext")));
                tv.setBackgroundColor(Color.parseColor(listLabel.getJSONObject(i).getString("color")));
                ll.addView(tv, params);
            }
            this.addView(ll, paramsll);
        }
    }

@SuppressWarnings("deprecation")
public void setClassicButton(JSONArray listButton, LinearLayout ll) throws JSONException {
    if (listButton.length() > 0)
    {
    DisplayMetrics metrics = new DisplayMetrics();
    ll = new LinearLayout(getContext());
    ll.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams paramsll = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    for (int i = 0; i < listButton.length(); i++) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) (getContext().getResources().getDisplayMetrics().widthPixels * listButton.getJSONObject(i).getDouble("size_x")), 
                (int) (getContext().getResources().getDisplayMetrics().heightPixels * listButton.getJSONObject(i).getDouble("size_y")));

        params.leftMargin = (int) (getContext().getResources().getDisplayMetrics().widthPixels * listButton.getJSONObject(i).getDouble("position_x"));
        params.topMargin = (int) (getContext().getResources().getDisplayMetrics().heightPixels * listButton.getJSONObject(i).getDouble("position_y"));

        int dest = listButton.getJSONObject(i).getInt("dest");
        MyButton myButton = new MyButton(context, dest);

        RoundRectShape rect = new RoundRectShape(
                  new float[] {50,50, 50,50, 50,50, 50,50},
                  null,
                  null);
                ShapeDrawable bg = new ShapeDrawable(rect);
                bg.getPaint().setColor(Color.parseColor(listButton.getJSONObject(i).getString("color")));

        myButton.setBackgroundDrawable(bg);
        myButton.setTextColor(Color.parseColor(listButton.getJSONObject(i).getString("colortext")));
        //BitmapDrawable bdra = new BitmapDrawable(downloadBitmap(listButton.getJSONObject(i).getString("http")));
        myButton.setText(listButton.getJSONObject(i).getString("text"));
        myButton.getBackground().setAlpha(30);
        myButton.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                MyButton button = (MyButton)view;
                Intent intent = new Intent(context, ClassicView.class);
                Content content = new Content(button.getDestination());
                intent.putExtra("CONTENT", content);

                context.startActivity(intent);
            }
        });
        ll.addView(myButton, params);
    }
    this.addView(ll, paramsll);
}
}
}

MyScrollView来自ScrollView。问题是下一个:

当我使用方法setClassicScrollView创建ScrollView时,我调用我的方法setClassicLabelsetClassicButton来在我的ScrollView中实现这两个视图。但我只能设置按钮或标签。我无法设置一个标签和一个按钮。我必须遇到一个来自布局的问题..我已经有两天了

以下是我的方法setClassicScrollView

public void setClassicScrollView(JSONArray listScrollView, RelativeLayout rl) throws JSONException {
        if (listScrollView.length() > 0) {
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);

            for (int i = 0; i  < listScrollView.length(); i++) {
                MyScrollView myScrollView = new MyScrollView(this, listScrollView);
                LinearLayout ll = new LinearLayout(this);
                ll.setOrientation(LinearLayout.VERTICAL);
                LinearLayout.LayoutParams paramsll = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) (metrics.widthPixels * listScrollView.getJSONObject(i).getDouble("size_x")), (int) (metrics.heightPixels * listScrollView.getJSONObject(i).getDouble("size_y")));
                params.leftMargin = (int) (metrics.widthPixels * listScrollView.getJSONObject(i).getDouble("position_x"));
                params.topMargin = (int) (metrics.heightPixels * listScrollView.getJSONObject(i).getDouble("position_y"));
                myScrollView.setBackgroundColor(Color.RED);
                myScrollView.setLayoutParams(params);
                myScrollView.setHorizontalScrollBarEnabled(true);
                myScrollView.getMaxScrollAmount();
                myScrollView.getBackground().setAlpha(50);

                //myScrollView.setClassicLabel(listScrollView.getJSONObject(i).getJSONArray("Label"), ll);
                myScrollView.setClassicButton(listScrollView.getJSONObject(i).getJSONArray("Button"), ll);

                rl.addView(myScrollView);
            }
        }
    }

有人知道如何在同一个ScrollView中设置TextViews和Buttons?

1 个答案:

答案 0 :(得分:0)

以下是滚动视图的布局。

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <Spinner
            android:id="@+id/spending_report_cycle_spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <View
            android:id="@+id/SomeView2"
            android:layout_width="wrap_content"
            android:layout_height="5dp"
            android:layout_gravity="center_horizontal"
            android:background="#008080"
            android:orientation="vertical" />

        <fragment
            android:id="@+id/fragment_content_1"
            android:name="com.mike.passintents.Fragment1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <View
            android:id="@+id/SomeView2"
            android:layout_width="wrap_content"
            android:layout_height="5dp"
            android:layout_gravity="center_horizontal"
            android:background="#008080"
            android:orientation="vertical" />

        <ListView
            android:id="@+id/spending_report_listview"
            android:layout_width="fill_parent"
            android:layout_height="300dp"
            android:background="#333333" >
        </ListView>
    </LinearLayout>
</ScrollView>

这里..在滚动视图中,我有一个线性布局作为一个孩子,之后我在线性布局中有其他视图。