android - 无法在对话框 fragment 中滚动

标签 android android-layout arcgis android-dialogfragment android-scrollview

我试图让 DialogFragment 弹出并显示可滚动的 TextView 图例,但它只是切断了图例的其余部分。我无法滚动浏览它或任何东西。我尝试添加一个 ScrollView 但它什么也没做。这是我得到的屏幕:

enter image description here

这是我的 XML 布局文件:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:scrollbars="vertical"
        android:scrollbarAlwaysDrawVerticalTrack="true"
        android:background="#fff">

        <TextView
            android:id="@+id/layer_legend_title_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:background="#fff"
            android:textColor="#000" />

        <ListView
            android:id="@+id/layer_legend_symbols_listview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:background="#fff"
            android:textColor="#000" />

    </LinearLayout>

</ScrollView>

我运行它来向 TextView 添加内容:

/**
 * A dialog that shows the legend of a ArcGISDynamicMapServiceLayer.
 */
public class LegendDialogFragment extends DialogFragment implements View.OnClickListener {

    public static final String TAG = LegendDialogFragment.class.getSimpleName();
    private LinearLayout mLinearLayout;
    private ArcGISDynamicMapServiceLayer mLayer;
    private ImageButton backButton;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mLinearLayout = (LinearLayout) inflater.inflate(R.layout.legend_dialog_fragment_layout, null);
        getDialog().setTitle(getActivity().getString(R.string.legend));

        mLayer = ((MainActivity) getActivity()).getLayer();

        backButton = (ImageButton) mLinearLayout.findViewById(R.id.backButton2);
        backButton.setOnClickListener(this);
        // before we can show the legend we have to fetch the legend info asynchronously
        new FetchLegendTask().execute();

        return mLinearLayout;
    }

    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if (!(activity instanceof MainActivity)) {
            throw new IllegalStateException("Hosting activity needs to be of type MainActivity");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.backButton2:
                getDialog().dismiss();
                break;
        }
    }

    /**
     * Retrieves the legend information asynchronously from the ArcGISDynamicMapServiceLayer.
     */
    private class FetchLegendTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            mLayer.retrieveLegendInfo();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            for (ArcGISLayerInfo layerInfo : mLayer.getAllLayers()) {
                if(layerInfo.isVisible()){
                    View view = getActivity().getLayoutInflater().inflate(R.layout.layer_legend_layout, null);
                    populateLegendView(view, layerInfo);

                    mLinearLayout.addView(view);
                }
            }
        }

        private View populateLegendView(View view, ArcGISLayerInfo layerInfo) {
            if (layerInfo != null) {
                TextView textView = (TextView) view.findViewById(R.id.layer_legend_title_textview);

                textView.setText(layerInfo.getName());
            }
            return view;
        }
    }

}

我希望整个 Fragment 都可以滚动,但不知道如何实现。每当我尝试包含 ScrollView 时,它只会滚动该特定行的其余部分。它不会显示所有内容。我认为它与 DialogFragment 有更多关系。

编辑:好的,所以我想我知道为什么它不会滚动。它在循环中一遍又一遍地调用 View。然后填充图层,最后将 TextView 添加到 LinearLayout。所以它不一定被“附加”。所以添加 ScrollView 只会使最后一部分可滚动。如果有人知道如何解决这个问题,请告诉我。

我从这里得到了例子的链接:

MapLegend

最佳答案

在对话 fragment 中

也许这是微不足道的,但对我来说它开始工作,在 FrameLayout 内包裹 ScrollView 之后

在 OnCreateView 中引入 DialogFragment

<FrameLayout
   <ScrollView
     <ConstainLayout...>

设置主题

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.RatingDialog2)
    }

样式

 <style name="RatingDialog2" parent="AppTheme">
            <item name="android:windowNoTitle">true</item>
            <item name="android:windowFullscreen">true</item>
            <item name="android:windowIsFloating">true</item>
            <item name="android:windowContentOverlay">@null</item>
            <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
            <item name="android:windowSoftInputMode">adjustPan</item>
            <item name="android:windowBackground">@android:color/transparent</item>
            <item name="android:windowMinWidthMinor">90%</item>
            <item name="android:windowMinWidthMajor">60%</item>
        </style>

关于android - 无法在对话框 fragment 中滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36269565/

相关文章:

android - 在打开/关闭软键盘之前,RecyclerView 不会更新

android - 在线性布局中扩展显示日期选择器对话框而不是 Activity

滚动后 Android ListView 项目动画

android - RecyclerView LinearLayoutManager 的 findLastCompletelyVisibleItemPosition() 与 findLastVisibleItemPosition() 方法

Android Spinner - onItemSelected/setOnItemSelectedListener 未触发

gis - Java 地理信息库

javascript - 是否可以将 JavaScript 函数添加到嵌入式网络 map 中?

python - 如何在 Python 中使用多个但数量有限的线程来处理列表

android - 使用房间数据库查询在产品之间进行搜索

android - 什么是 setContentView(R.layout.main)?