LinearLayout里面的水平滚动视图 - Android

时间:2013-03-18 15:22:21

标签: android android-layout scrollview horizontal-scrolling horizontalscrollview

我有一个自定义列表。我试图只横向滚动特定的部分。对于Instance,在下图中我试图水平滚动只是橙色框。

enter image description here

这是我的XML布局:在XML布局中,橙色框开始“android:id =”@ + id / projected“

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="63dp"
    android:layout_marginLeft="0dp"
    android:background="@drawable/bkg_img_fullcell"
    android:orientation="horizontal" >

    <!-- Left Column -->

    <LinearLayout
        android:id="@+id/widget_lineupview_playerdata_container"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/widget_lineup_table_row_height"
        android:layout_toRightOf="@+id/sideView"
        android:gravity="center_vertical"
        android:layout_toLeftOf="@+id/projected"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="4" >

            <!-- rel 1 -->

            <LinearLayout
                android:id="@+id/playername1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:layout_toRightOf="@+id/sideView">

                <com.cbssports.nflapp.ffb.MyTextViewBold
                    android:id="@+id/widget_lineupview_playerdata_playername"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ellipsize="end"
                    android:inputType="text|textNoSuggestions"
                    android:layout_marginLeft="5dp"
                    android:maxLines="1"
                    android:text=""
                    android:textColor="#08233e"
                    android:textSize="15dp" />


            </LinearLayout>
            <!-- rel 2 -->

            <LinearLayout
                android:id="@+id/widget_lineupview_playerdata_playerStatsIconContainer"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_toRightOf="@+id/playername1"
                android:orientation="horizontal" >

                <ImageView
                    android:id="@+id/widget_lineupview_playerdata_statusIconSuspended"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="1dp"
                    android:src="@drawable/btn_s"
                    android:visibility="gone" />
            </LinearLayout>
 </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_toRightOf="@+id/sideView" >

            <TextView
                    android:id="@+id/widget_lineupview_playerdata_playerPositionTeam"
                    android:layout_marginLeft="5dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ellipsize="end"
                    android:inputType="text|textNoSuggestions"
                    android:maxLines="1"
                    android:text=""
                    android:textColor="#333333"
                    android:textSize="12dp" />


            <ImageView
                android:id="@+id/widget_lineupview_playerdata_startingPitcherStatus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp"
                android:src="@drawable/btn_baseballgraphic"
                android:visibility="gone" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/sideView"
        android:layout_width="@dimen/widget_lineup_table_status_column_width"
        android:layout_height="@dimen/widget_lineup_table_row_height"
        android:layout_alignParentLeft="true"
        android:background="@drawable/bkg_img_rosterpos"
        android:gravity="center" >

        <!-- <Button -->
        <com.cbssports.nflapp.ffb.VerticalTextView
            android:id="@+id/widget_lineupview_playerdata_playerStatusButton"
            android:layout_width="21dp"
            android:layout_height="60dp"
            android:layout_margin="0dp"
            android:padding="0dp"
            android:textColor="#004a8e"
            android:textSize="11dp"
            android:textStyle="bold"
            android:gravity="bottom|center" />
    </LinearLayout>
***<LinearLayout
        android:id="@+id/projected"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/widget_lineup_table_row_height"
        android:background="@drawable/bkg_img_whiteoverlay"
        android:layout_toLeftOf="@+id/percentStarted">

        <TextView
            android:id="@+id/widget_lineupview_player_h2h_stats_started_custom"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:textStyle="bold"
            android:textSize="16dp"
            android:textColor="#333333" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/percentStarted"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/widget_lineup_table_row_height"
        android:background="@drawable/bkg_img_whiteoverlay"
         android:layout_alignParentRight="true"
         android:layout_marginLeft="2dp">

        <TextView
            android:id="@+id/widget_lineupview_player_projected_custom"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:textStyle="bold"
            android:textSize="16dp"
            android:textColor="#333333" />
    </LinearLayout>***
</RelativeLayout>

2 个答案:

答案 0 :(得分:4)

谢谢g00dy,我可以通过在horizo​​ntalScrollView中添加一个LinearLayout来解决它。

这是我的解决方案。

   <HorizontalScrollView
                android:id="@+id/horizontalScroll"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:animateLayoutChanges="true"
                android:scrollbars="none">
   <LinearLayout
                android:id="@+id/horizontalScroll_linear"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_alignParentRight="true" >
         <LinearLayout
             android:id="@+id/projected"
             android:layout_width="wrap_content"
             android:layout_height="@dimen/widget_lineup_table_row_height"
             android:background="@drawable/bkg_img_whiteoverlay"
             android:layout_toLeftOf="@+id/percentStarted">

                <TextView
                    android:id="@+id/widget_lineupview_player_h2h_stats_started_custom"
                    android:layout_width="50dp"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:gravity="center"
                    android:textStyle="bold"
                    android:textSize="16dp"
                    android:textColor="#333333" />
           </LinearLayout>
        <LinearLayout
            android:id="@+id/percentStarted"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/widget_lineup_table_row_height"
            android:background="@drawable/bkg_img_whiteoverlay"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="2dp">

             <TextView
                android:id="@+id/widget_lineupview_player_projected_custom"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:gravity="center"
                android:textStyle="bold"
                android:textSize="16dp"
                android:textColor="#333333" />
      </LinearLayout>
</LinearLayout>
</HorizontalScrollView>

答案 1 :(得分:4)

您还可以将LinearLayout fillViewport设置为true,将foregroundGravity设置为true(不确定是否需要此参数),并将子LinearLayout引力设置为右:

<HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="@dimen/IconM"
        android:id="@+id/account_food_scroll"
        android:layout_alignParentEnd="false"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/client_add_button_layout"
        android:foregroundGravity="right"
        android:layout_marginLeft="@dimen/PadM"
        android:fillViewport="true">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/IconM"
            android:id="@+id/account_food_layout"
            android:gravity="right">
            </LinearLayout>
    </HorizontalScrollView>