Android ScrollView从选项卡栏开始,但需要从布局顶部滚动

Android ScrollView从选项卡栏开始,但需要从布局顶部滚动,android,layout,scrollview,android-tabhost,Android,Layout,Scrollview,Android Tabhost,我的布局中的滚动条有问题。 我的布局包含一个滚动条,其中包含两个布局。第一个布局具有静态高度。第二个latout包含tabHost for tabgroup。第二个布局还包含可扩展的Gridview 问题是,当我加载布局时,它直接显示Tabbar并隐藏第一个布局。我们可以滚动,但最初我需要显示顶部布局,而不是tabHost。有人能帮我解决这个问题吗 先谢谢你 我已经从xml文件中添加了代码片段 <ScrollView android:id="@+id/scrollView1"

我的布局中的滚动条有问题。 我的布局包含一个滚动条,其中包含两个布局。第一个布局具有静态高度。第二个latout包含tabHost for tabgroup。第二个布局还包含可扩展的Gridview

问题是,当我加载布局时,它直接显示Tabbar并隐藏第一个布局。我们可以滚动,但最初我需要显示顶部布局,而不是tabHost。有人能帮我解决这个问题吗

先谢谢你

我已经从xml文件中添加了代码片段

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/header"
    android:fillViewport="true" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <RelativeLayout
            android:id="@+id/relay1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="true" >

            <include
                android:id="@+id/cover"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                layout="@layout/coverphoto" />

            <ImageView
                android:id="@+id/ximg_logo"
                android:layout_width="70dip"
                android:layout_height="70dip"
                android:layout_below="@+id/cover"
                android:layout_marginLeft="16dip"
                android:layout_marginTop="-42dip" />

            <TextView
                android:id="@+id/xtxt_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/ximg_logo"
                android:layout_marginLeft="5dip"
                android:layout_toRightOf="@+id/ximg_logo"
                android:ellipsize="end"
                android:ems="5"
                android:singleLine="true"
                android:textColor="@color/sixty_black"
                android:textSize="20sp" >

                <requestFocus />
            </TextView>

            <Button
                android:id="@+id/xbtn_follow"
                style="@style/follow_button_normal"
                android:layout_width="95dip"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/cover"
                android:layout_marginRight="7dip"
                android:background="@drawable/buttons_shape_fill_orange"
                android:text="@string/str_follow" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/cover"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:background="@color/fifty_black"
                android:orientation="vertical"
                android:paddingLeft="3dip"
                android:paddingRight="3dip"
                android:weightSum="3" >

                <RelativeLayout
                    android:id="@+id/xlay_following"
                    android:layout_width="match_parent"
                    android:layout_height="0dip"
                    android:layout_alignParentTop="true"
                    android:layout_marginTop="10dip"
                    android:layout_weight="1"
                    android:onClick="following" >

                    <TextView
                        android:id="@+id/xtxt_imfollowing"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_centerHorizontal="true"
                        android:textColor="@color/white"
                        android:textSize="18sp" />

                    <TextView
                        android:id="@+id/xtxt_imfollowing_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/xtxt_imfollowing"
                        android:layout_centerHorizontal="true"
                        android:padding="5dip"
                        android:text="@string/str_im_following"
                        android:textColor="@color/white"
                        android:textSize="14sp" />
                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/xlay_follower"
                    android:layout_width="match_parent"
                    android:layout_height="0dip"
                    android:layout_weight="1"
                    android:onClick="follower" >

                    <TextView
                        android:id="@+id/xtxt_myfollower"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="10dip"
                        android:textColor="@color/white"
                        android:textSize="18sp" />

                    <TextView
                        android:id="@+id/xtxt_myfollowers_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/xtxt_myfollower"
                        android:layout_centerHorizontal="true"
                        android:padding="5dip"
                        android:text="@string/str_myfollowers"
                        android:textColor="@color/white"
                        android:textSize="14sp" />
                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/xlay_interests"
                    android:layout_width="match_parent"
                    android:layout_height="0dip"
                    android:layout_alignParentBottom="true"
                    android:layout_below="@+id/xlay_follower"
                    android:layout_centerHorizontal="true"
                    android:layout_weight="1" >

                    <TextView
                        android:id="@+id/xtxt_interests"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:layout_marginTop="10dip"
                        android:textColor="@color/white"
                        android:textSize="18sp" />

                    <TextView
                        android:id="@+id/xtxt_interests_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/xtxt_interests"
                        android:layout_centerHorizontal="true"
                        android:paddingBottom="5dip"
                        android:text="@string/str_interest"
                        android:textColor="@color/white"
                        android:textSize="14sp" />
                </RelativeLayout>
            </LinearLayout>

            <RelativeLayout
                android:id="@+id/xrl_msg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:background="@color/fifty_black"
                android:padding="10dip" >

                <ImageView
                    android:id="@+id/ximg_message"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:src="@drawable/ic_msg" />
            </RelativeLayout>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/relay2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/relay1"
            android:layout_marginTop="10dip" >

            <TabHost
                android:id="@android:id/tabhost"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

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

                    <TabWidget
                        android:id="@android:id/tabs"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="20dip" >
                    </TabWidget>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dip"
                        android:background="@color/orange" />

                    <FrameLayout
                        android:id="@android:id/tabcontent"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >

                        <LinearLayout
                            android:id="@+id/tab1"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="vertical" >
                        </LinearLayout>

                        <LinearLayout
                            android:id="@+id/tab2"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="vertical" >
                        </LinearLayout>

                        <LinearLayout
                            android:id="@+id/tab3"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="vertical" >
                        </LinearLayout>
                    </FrameLayout>
                </LinearLayout>
            </TabHost>
        </RelativeLayout>
    </RelativeLayout>
</ScrollView>

当然,RelativeLayout rl1与rl2重叠。它们都是布局宽度上的匹配父项。您需要使用android:layout\u alignParentTop或android:layout\u alignParentLeft=true将两个布局与左/右或上/下对齐。 但是,您不能将其用于RelativeLayout。它用于RelativeLayout或LinearLayout中的UI元素


由于您没有使用选项卡元素或任何UI发布完整布局文件,因此我无法提供任何xml示例。

我有一个类似的问题,我的布局的一种方法是使用布局模式更改高度和可见性,如下所示:

<RelativeLayout
    android:id="@+id/relay1"
    android:layout_width="match_parent"
    android:focusable="true"

    android:layout_height="0dp"
    android:visibility="gone"
    >
请注意setVisibility和有效常量

现在,使用示例代码设置视图的高度。这可能是您需要的唯一代码,实际上可能工作得更好

ViewGroup.LayoutParams layoutParams = relayLayout.getLayoutParams();
layoutParams.height = 0;
relayLayout.setLayoutParams(layoutParams);

请注意ViewGroup.LayoutParams和height属性。在这段代码中,布局将被隐藏,第二个布局将上升到顶部。

另一个解决方案,可能是最好的解决方案是谷歌的答案,显示/隐藏两个不同的布局。在您的布局文件中,我看到两种非常不同的布局设计,因此这适用于您的情况。 一个网页说明了这一点。 这本书读起来很好

下面是一个示例代码,让这个想法起作用:

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();
示例代码来自

这种代码和布局设计的一个优点是,您的两个布局至少在两个文件中分开,并且在将来的代码更改中强制分开。
玩得开心:-Tommy Kwee。

我认为如果你只发布完整的布局会有所帮助,除非它太大,希望不会。我需要从上到下滚动。所以我认为匹配父母不是宽度的问题。在高度方面,我只提供了wrap_内容。我的第二个布局中有tabHost。这可能是我的问题所在。如果使用tabHost,则滚动位置将从tabHost移动。请给我提供一些例子,它应该包含一个布局下面的布局,它应该包含与gridView项目tabHost,我已经更新了我的布局xml。。。你能给我一个解决方案吗???由于你在这个问题上的更新,我有一个更好的理解,我希望。我发布了两个不同的答案,片段解决方案和最简单的动态布局解决方案。我没有使用片段。只要给我一个从布局顶部滚动的解决方案。我使用了很多方法,但仍然无法得到解决方案。希望你能帮助我。奎师那,你的回答太晚了,现在我不记得是什么问题了。以前的答案是什么?它们有帮助吗?
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();