如何将活动集中到其顶级元素

时间:2017-08-14 11:27:48

标签: android

我有一个活动,其中,顶部我有一个图像滑块,在这下面的网格形式的布局几乎涵盖了我的所有手机的屏幕,最后我有一个列表视图尺寸接近我手机屏幕的一半。(我使用滚动视图滚动元素)

总而言之,它们几乎是我手机屏幕的两倍。

现在每当我开始这项活动时,屏幕会自动聚焦到我的元素的底部。简单来说,它不会向我显示图像滑块和网格视图的上半部分,它只显示网格视图的下半部分和列表视图。 所以当我开始这个时我需要手动滚动它。

我搜索了很多,但没有任何办法将它自动​​集中到我活动的主要元素。请建议我现在该怎么办。

提前谢谢。

这是我的xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.gehlot.nitin.kitchenspices.Main2Activity"

    android:id="@+id/scrollView"
    tools:showIn="@layout/app_bar_main2">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="beforeDescendants"
        android:orientation="vertical">
        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_margin="10dp"
            android:layout_weight="1.0"
            android:id="@+id/viewPager"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_weight="1.0"
            android:text="Shop catagory"
            style="@style/textStyle"
            android:layout_gravity="center"
            android:gravity="center"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:orientation="vertical"
            android:background="@drawable/back"
            android:layout_weight="1.0">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:layout_weight="1.0">
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1.0"
                    android:layout_margin="5dp"
                    android:scaleType="fitXY"
                    android:background="@drawable/back"
                    android:id="@+id/img1"/>
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1.0"
                    android:layout_margin="5dp"
                    android:scaleType="fitXY"
                    android:background="@drawable/back"
                    android:id="@+id/img2"/>
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1.0"
                    android:background="@drawable/back"
                    android:layout_margin="5dp"
                    android:scaleType="fitXY"
                    android:id="@+id/img3"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:layout_weight="1.0">
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1.0"
                    android:background="@drawable/back"
                    android:layout_margin="5dp"
                    android:scaleType="fitXY"
                    android:adjustViewBounds="true"
                    android:id="@+id/img4"/>
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1.0"
                    android:adjustViewBounds="true"
                    android:scaleType="fitXY"
                    android:background="@drawable/back"
                    android:layout_margin="5dp"
                    android:id="@+id/img5"/>
                <ImageView
                    android:adjustViewBounds="true"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1.0"
                    android:scaleType="fitXY"
                    android:background="@drawable/back"
                    android:layout_margin="5dp"
                    android:id="@+id/img6"/>
                </LinearLayout>
          </LinearLayout>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:id="@+id/lv_main"
        android:layout_below="@id/img4">
    </ListView>
</LinearLayout>
</ScrollView>

2 个答案:

答案 0 :(得分:0)

尝试将此行添加到xml中ScrollView的直接子级。它会阻止自动滚动到底部。

android:descendantFocusability="beforeDescendants"

示例:

<android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

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


                //children views

            </LinearLayout>


</android.support.v4.widget.NestedScrollView>

答案 1 :(得分:0)

将其添加到xml中的根布局:

android:id="@+id/root_layout"

在你的Acivity.java中添加:

ViewGroup layout = (ViewGroup) findViewById(R.id.layout);
layout.requestFocus();