Android 如何在scrollview中动态添加可滚动的水平线布局

Android 如何在scrollview中动态添加可滚动的水平线布局,android,Android,我试图在方法中动态地在scrollview内部实现一个可滚动的水平Linearlayout,但是视图(Linearlayout)不滚动 该方法从服务器获取一些结果,然后动态创建视图。“layout_services”是在xml文件中实现的垂直线性布局 瞧,代码: for (ServiceResult serviceResult : response.getResult()) { LinearLayout.LayoutParams textViewParams = new Lin

我试图在方法中动态地在scrollview内部实现一个可滚动的水平Linearlayout,但是视图(Linearlayout)不滚动

该方法从服务器获取一些结果,然后动态创建视图。“layout_services”是在xml文件中实现的垂直线性布局

瞧,代码:

 for (ServiceResult serviceResult : response.getResult()) {
        LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        textViewParams.setMargins(15, 15, 15, 15);
        textViewParams.gravity = Gravity.RIGHT;
        TextView textView = new TextView(getActivity());
        textView.setPadding(5, 5, 5, 5);
        textView.setText(serviceResult.getCategory());
        textView.setLayoutParams(textViewParams);
        textView.setTextColor(Color.WHITE);
        textView.setBackgroundColor(Color.LTGRAY);
        if (!serviceResult.getServices().isEmpty()) {
            layout_services.addView(textView);
            LinearLayout.LayoutParams scrollLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            ScrollView scrollView = new ScrollView(getActivity());
            scrollView.setLayoutParams(scrollLayoutParams);
            LinearLayout linearLayout = new LinearLayout(getActivity());
            linearLayout.setPadding(5, 5, 5, 5);
            LinearLayout.LayoutParams horizontalLayoutParams =
                    new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
            horizontalLayoutParams.gravity = Gravity.RIGHT;
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            linearLayout.setLayoutParams(horizontalLayoutParams);
            scrollView.addView(linearLayout);
            for (int i = 0; i < serviceResult.getServices().size(); i++) {
                int finalI = i;
                Button btn = new Button(getActivity());
                btn.setText(serviceResult.getServices().get(finalI).getName());
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(300, 200);
                params.setMargins(15, 15, 15, 15);
                params.gravity = Gravity.LEFT;
                btn.setLayoutParams(params);
                btn.setTextColor(Color.BLACK);
                btn.setOnClickListener(view -> {
                });
                linearLayout.addView(btn);
            }
            layout_services.addView(scrollView);
        }
    }
for(servicesult-servicesult:response.getResult()){
LinearLayout.LayoutParams textViewParams=新建LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_父级,ViewGroup.LayoutParams.WRAP_内容);
textViewParams.setMargins(15,15,15,15);
textViewParams.gravity=gravity.RIGHT;
TextView TextView=新的TextView(getActivity());
setPadding(5,5,5,5);
setText(servicesult.getCategory());
setLayoutParams(textViewParams);
textView.setTextColor(Color.WHITE);
textView.setBackgroundColor(Color.LTGRAY);
如果(!serviceResult.getServices().isEmpty()){
layout_services.addView(文本视图);
LinearLayout.LayoutParams scrollLayoutParams=新的LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_父级,ViewGroup.LayoutParams.MATCH_父级);
ScrollView ScrollView=新的ScrollView(getActivity());
scrollView.setLayoutParams(scrollLayoutParams);
LinearLayout LinearLayout=新的LinearLayout(getActivity());
线性布局。设置填充(5,5,5,5);
LinearLayout.LayoutParams horizontalLayoutParams=
新建LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_父对象,
ViewGroup.LayoutParams.WRAP_内容);
horizontalLayoutParams.gravity=gravity.RIGHT;
linearLayout.setOrientation(linearLayout.HORIZONTAL);
linearLayout.setLayoutParams(horizontalLayoutParams);
scrollView.addView(线性布局);
对于(int i=0;i{
});
线性布局。添加视图(btn);
}
layout_services.addView(滚动视图);
}
}

使用
水平滚动视图
而不是
滚动视图
作为要水平滚动的线性布局的父级

使用
水平滚动视图
而不是
滚动视图
作为要水平滚动的线性布局的父级

您可以尝试嵌套滚动视图


阅读此处的文档:

您可以尝试嵌套滚动视图


阅读此处的文档:

始终使用水平滚动视图来水平滚动始终使用水平滚动视图来水平滚动