ScrollView无法在Android中的XML中完全向下滚动

时间:2019-05-02 04:47:05

标签: android android-layout android-xml android-scrollview

我有一个XML代码,用于显示团队的板球记分卡。 这是我的scorecard.xml代码,我在Scrollview的Linear Layout下使用NonScrollableList:

<?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="wrap_content"
    android:layout_weight="1"
    android:fillViewport="true"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/windowBackground"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="50.0dip"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip"
            android:background="@drawable/scorecard_uperlayout">

            <TextView
                android:id="@+id/Batteam_description"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginStart="10.0dip"
                android:layout_marginLeft="10.0dip"
                android:text="Aus"
                android:textColor="@color/textColorPrimary"
                android:textSize="18.0sp" />
        </RelativeLayout>

        <LinearLayout
            android:id="@id/tabllayout"
            android:layout_width="fill_parent"
            android:layout_height="40.0dip"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip"
            android:background="@color/graycolor2"
            android:visibility="visible"
            android:weightSum="11.0">

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="5.5">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="10.0dip"
                    android:layout_marginLeft="10.0dip"
                    android:text="Batsman"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="R"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="B"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="4s"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="6s"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="2.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="SR"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip">

            <com.example.live_score.activity.NonScrollableList
                android:id="@+id/list"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10.0dip"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip"
            android:background="@color/layoutBackground"
            android:visibility="visible">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10.0dip"
                android:layout_marginLeft="10.0dip"
                android:text=" Extras"
                android:textColor="@color/textcolor"
                android:textSize="16.0sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10.0dip"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip"
            android:background="@color/layoutBackground"
            android:visibility="visible">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10.0dip"
                android:layout_marginLeft="10.0dip"
                android:text="Yet to Bat"
                android:textColor="@color/textcolor"
                android:textSize="16.0sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/tabllayout_bowl"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip"
            android:background="@color/graycolor2"
            android:visibility="visible"
            android:weightSum="11.0">

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="5.3">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginStart="10.0dip"
                    android:layout_marginLeft="10.0dip"
                    android:text="Bowler"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="O"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="M"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="R"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="W"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Wd"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Nb"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip"
            android:visibility="visible">

            <com.example.live_score.activity.NonScrollableList
                android:id="@+id/list_bowlings"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="40.0dip"
            android:layout_marginLeft="10.0dip"
            android:layout_marginTop="5.0dip"
            android:layout_marginRight="10.0dip"
            android:background="@color/graycolor2"
            android:visibility="visible"
            android:weightSum="9.0">

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="6.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_gravity="center"
                    android:layout_marginStart="10.0dip"
                    android:layout_marginLeft="10.0dip"
                    android:text="Fall of Wickets"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="W"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="R"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1.0">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="O"
                    android:textColor="@color/textcolor2"
                    android:textSize="18.0sp" />
            </RelativeLayout>

        </LinearLayout>

    </LinearLayout>
</ScrollView>

以下是NonScrollableList活动代码:

public class NonScrollableList extends ListView {

    public NonScrollableList(Context context) {
        super(context);
    }
    public NonScrollableList(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public NonScrollableList(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
}

下面是上面代码的onCreateView:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.scorecard, container, false);
        listView = (NonScrollableList) rootView.findViewById(R.id.list);
        list_bowlings = (NonScrollableList) rootView.findViewById(R.id.list_bowlings);
}

但问题是我无法完全向下滚动。我需要在哪里增加更多高度来固定滚动条?有人可以帮忙吗?预先感谢

3 个答案:

答案 0 :(得分:0)

尝试使用此自定义ListView-

  

自定义ListView:

public class MyCustomListView extends ListView {

    public MyCustomListView(Context context) {
        super(context);
    }

    public MyCustomListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyCustomListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int heightSpec;

        if (getLayoutParams().height == LayoutParams.FILL_PARENT) {
            heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        } else {
            heightSpec = heightMeasureSpec;
        }

        super.onMeasure(widthMeasureSpec, heightSpec);
    }
}
  

在XML中:

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

    <LinearLayout

        android:id="@+id/slistLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
                android:id="@+id/Batteam_description"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginStart="10.0dip"
                android:layout_marginLeft="10.0dip"
                android:text="Aus"
                android:textColor="@color/textColorPrimary"
                android:textSize="18.0sp" />


        <your_packagename.MyCustomListView
            android:id="@+id/pListView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:clickable="true"
            android:descendantFocusability="blocksDescendants"
            android:fadeScrollbars="false"
            android:focusable="true"
            android:scrollbarAlwaysDrawVerticalTrack="true"
            android:scrollbarStyle="insideInset" />


        <your_packagename.MyCustomListView
            android:id="@+id/pListView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:clickable="true"
            android:descendantFocusability="blocksDescendants"
            android:fadeScrollbars="false"
            android:focusable="true"
            android:scrollbarAlwaysDrawVerticalTrack="true"
            android:scrollbarStyle="insideInset" />


    </LinearLayout>


</ScrollView>

答案 1 :(得分:0)

只需使用 android:nestedScrollingEnabled =“ true” 在ListView中

答案 2 :(得分:0)

在您的 xml 代码中,确保ScrollView内只有一个孩子,并将LinearLayoutScrollView的高度从{{1} }到"wrap_content"