【问题标题】:ScrollView starts from tabbar, but needs to scroll from top of the layoutScrollView 从 tabbar 开始,但需要从布局顶部滚动
【发布时间】:2015-02-24 05:55:14
【问题描述】:

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

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

在此先感谢...

我已经从 xml 文件中添加了我的代码 sn-p。

<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>

【问题讨论】:

  • 我认为如果你只发布完整的布局会有所帮助,除非它太大,希望不会。
  • 我需要从上到下滚动。所以我认为“match_parent”不是宽度的问题。在高度中,我只给出了 wrap_content。我的第二个布局中有 tabHost。这可能是我的问题。如果我使用 tabHost,那么滚动位置会从 tabHost 移动。请给我一些例子,它应该包含一个布局,它应该包含 tabHost 和 gridView 项目
  • 您好,我已经更新了我的布局 xml...您能建议我一个解决方案吗???
  • 由于您在问题中的更新,我希望有更好的理解。我发布了两个不同的答案,片段解决方案和动态布局解决方案(最容易做到)。

标签: android layout scrollview android-tabhost


【解决方案1】:

当然,RelativeLayout rl1 与 rl2 重叠。它们都是 layout_width 上的 match_parent。您需要使用android:layout_alignParentTopandroid:layout_alignParentLeft="true" 将两个布局与左/右或上/下对齐。 但是你不能将它用于RelativeLayout。它用于RelativeLayout或LinearLayout中的UI元素。

由于您没有发布带有标签元素或任何 UI 的完整布局文件,因此我无法给您任何 xml 示例。

【讨论】:

    【解决方案2】:

    我有一个类似的问题,一种适用于我的布局的方法是更改​​高度和可见性,如下所示(使用您的布局架构作为一个很好的例子):

    <RelativeLayout
        android:id="@+id/relay1"
        android:layout_width="match_parent"
        android:focusable="true"
    
        android:layout_height="0dp"
        android:visibility="gone"
        >
    

    注意 android:layout_height可见性。当然我知道你不希望你的第一个布局总是被绝对隐藏。但这就是想法,您可以对第二个布局 relay2 执行相同的操作。

    这是一个动态隐藏/显示布局的示例代码。对象 relayLayout 是您的 relay1 视图。

    if "hide the layout" {
      relayLayout.setVisibility(LinearLayout.GONE);
    }
    else {
      relayLayout.setVisibility(LinearLayout.VISIBLE);
    }
    

    注意setVisibility 和有效常量。

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

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

    注意ViewGroup.LayoutParamsheight 属性,主要是。在这段代码中,布局将被隐藏,第二个布局应该上升到顶部。

    【讨论】:

      【解决方案3】:

      另一种解决方案可能是最好的解决方案是 Google 的显示/隐藏 2 种不同布局的答案。在您的布局文件中,我看到 2 个非常不同的布局设计,因此这适用于您的情况。 一个说明这一点的网页是Building a Dynamic UI with Fragments。 很好看。

      这是一个示例代码,使这个想法生效:

      // 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();
      

      示例代码来自Fragments

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

      【讨论】:

      • 我没有使用 Fragments。只需给我一个从布局顶部滚动的解决方案。我已经使用了很多方法,但仍然无法得到解决方案。希望你能帮助我。
      • 克里希纳,你的回复太迟了,现在我不记得问题是什么了。前面的答案如何?它们有帮助吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多