繁体   English   中英

从水平ScrollView中的展开式布局获取多个视图

[英]Multiple Views from Inflated Layout in horizontal ScrollView

抱歉,如果我无法正确传达我的问题,但是自从我为Android开发以来已经有一段时间了。

我正在尝试做的是在屏幕顶部具有多个选项卡,这些选项卡在按下时会显示ScrollView。 这不是我想要使用ViewPager的东西,因为滑动操作需要与选项卡分开。

每个ScrollView应该有一个子元素,该子元素是LinearLayout(水平方向),该子元素应采用ScrollView的高度,但宽度应包裹所包含的内容。 (内容的宽度应与屏幕的宽度匹配)。

选项卡的“片段活动”内已膨胀的“内容视图”应并排放置,每个视图占据了选项卡内容的全部空间。

以下是我尝试描述的内容以及所创建的无效布局的代码段的说明。

在此处输入图片说明

片段布局-(tab_fragment.xml)

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/tabScrollView"
        android:fillViewport="true">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:id="@+id/pageLinearLayout"
            ></LinearLayout>
</ScrollView>

膨胀视图-(article_layout.xml)

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ff0000">
</ImageView>

片段活动

// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.tab_fragment, container, false);

LinearLayout pageLinearLayout = (LinearLayout)view.findViewById(R.id.pageLinearLayout);

for(int i = 0; i < 3; i++){
    View pageView = inflater.inflate(R.layout.article_layout, null);
    if(i % 2 != 0){
        pageView.setBackgroundColor(Color.parseColor("#00ffff"));
    }
    pageLinearLayout.addView(pageView);
}

我添加了一个名为scrollmain(仅包含LinearLayout)的布局文件,并且我测试了以下代码,它运行良好:

 //View view = inflater.inflate(R.layout.tab_fragment, container, false);
    LinearLayout layout = (LinearLayout) findViewById(R.id.scrollmain);
    LayoutInflater inflater = getLayoutInflater();
    View view = inflater.inflate(R.layout.tab_fragment, null, false);
    layout.addView(view)

    LinearLayout pageLinearLayout = (LinearLayout)view.findViewById(R.id.pageLinearLayout);

    for(int i = 0; i < 3; i++){
        View pageView = inflater.inflate(R.layout.article_layout, null);
        if(i % 2 != 0){
            pageView.setBackgroundColor(Color.parseColor("#00ffff"));
        }
        pageLinearLayout.addView(pageView);
    }

祝好运。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM