【问题标题】:ScrollView not working with fragmentScrollView 不适用于片段
【发布时间】:2014-05-28 23:12:44
【问题描述】:

我正在尝试创建一个动态片段。我在里面添加了 30 个按钮,我希望它是可滚动的。

这是我的片段 xml 代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_orange_light"
    tools:context="com.viewpagerexample.app.FragmentA">

            <ScrollView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/scrollView">

                <TableLayout
                    android:id="@+id/table_layout"
                    android:layout_width="match_parent"
                    android:layout_height="fill_parent"
                    android:baselineAligned="false"
                    android:clickable="false"
                    android:focusable="false"
                    android:focusableInTouchMode="false">
                </TableLayout>
            </ScrollView>
</LinearLayout>

这是我的片段的代码:

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_a,container,false);
        // Inflate the layout for this fragment
        int numberOfButtons= getArguments().getInt("someInt",0);
        linearLayout = new LinearLayout(v.getContext());
        view = new TableLayout(v.getContext());
        TableRow tr;
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        // Inflate the layout for this fragment
        view.setOrientation(TableLayout.VERTICAL);
        view.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams
                .MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
        TableLayout.LayoutParams layoutParams =  new TableLayout.LayoutParams
                (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(20,20,20,20);
        int numberOfRows = numberOfButtons/2;
        int masaCounter = 0;
        for (int i = 0;i<numberOfRows;i++)
        {

            tr = new TableRow(view.getContext());
            tr.setLayoutParams(layoutParams);
            for (int j= 0;j<2;j++)
            {
                masaCounter++;
                btn = new Button(getActivity());
                btn.setBackgroundResource(R.drawable.buttonstyle);
                btn.setHeight(200);
                btn.setText("Masa" + masaCounter);
                btn.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams
                        .MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 1));
                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                tr.addView(btn);
            }
            view.addView(tr);
        }
        linearLayout.addView(view);
        myView = linearLayout;
        return myView;
    }

我添加了按钮,但我看不到所有按钮。因为滚动视图不起作用。我找不到我做错了什么。我怎样才能让它工作?

【问题讨论】:

    标签: android android-layout android-fragments


    【解决方案1】:

    改变父布局和ScrollView的高、宽为match_parent并添加属性

     android:fillViewport="true"
    

    在 ScrollView 中,将表格布局的高度、宽度更改为 wrap_content 和 fill_parent 相关性将解决您的问题。

    【讨论】:

      【解决方案2】:

      尝试将ScrollView 作为布局上的rootView 并将LinearLayout 和所有其他Views 放入其中。

      像这样:

      <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/scrollView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:background="@android:color/holo_orange_light"
      tools:context="com.viewpagerexample.app.FragmentA" >
      
      <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" >
      
          <TableLayout
              android:id="@+id/table_layout"
              android:layout_width="match_parent"
              android:layout_height="fill_parent"
              android:baselineAligned="false"
              android:clickable="false"
              android:focusable="false"
              android:focusableInTouchMode="false" >
          </TableLayout>
      </LinearLayout>
      

      【讨论】:

      • 感谢您的建议。我已经尝试过了,但它对我不起作用。
      • 如果你在 上设置一个固定的高度值,比如 600dp,它会起作用,我自己试过了。尝试在您的片段上没有代码。
      • 是的,我做到了。它对我不起作用。 :/ 我解决了我在代码端添加这些行的问题scrollView = new ScrollView(getActivity());scrollView.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT, ScrollView.LayoutParams.FILL_PARENT)); scrollView.addView(view); myView = scrollView; return myView;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多