【问题标题】:Android Scrollview layout issueAndroid Scrollview 布局问题
【发布时间】:2014-05-02 12:42:02
【问题描述】:

截图:

我正在开发这个 android 主题模板。它的布局上有一个滚动视图,以便卡片可以垂直滚动。我面临的问题是,每当我打开应用程序时,它都会像图像一样打开。 1 即自动滚动到底部,而我希望它在顶部打开,就像在图像编号中一样。 2. 我是编码初学者,所以对布局了解不多。

这里是 gridview_main.xml 代码

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/app_bg" >

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

        <include
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/feature_banner" />

        <FrameLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="@dimen/list_padding"
            android:layout_gravity="center"
            android:id="@+id/container" />

    </LinearLayout>

</ScrollView>

这里是 gridview_layout.xml 代码。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center" 
    tools:context="listview.Main">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/list_height"
        android:background="@drawable/card_bg_pressed"
        android:orientation="horizontal"
        android:padding="@dimen/list_icon"
        tools:ignore="UselessParent" >

        <ImageView
            android:id="@+id/list_image"
            android:contentDescription="@string/content_description"
            android:layout_width="@dimen/listview_icon"
            android:layout_height="@dimen/listview_icon"
            android:layout_marginLeft="@dimen/list_icon"
            android:layout_marginRight="@dimen/list_icon_right"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/icon_email" />

        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/title"
            android:layout_marginTop="-1dp"
            android:layout_marginRight="@dimen/list_padding"
            android:layout_toRightOf="@+id/list_image"
            android:text="Join our Google+ Community and share your themes or find themes     
            android:textSize="@dimen/list_desc_text"
            android:textColor="@color/list_desc_color" 
            tools:ignore="HardcodedText"/>

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/description"
            android:layout_alignParentTop="true"
            android:layout_marginTop="6dp"
            android:text="Community"
            android:textColor="@color/list_title_color"
            android:textSize="@dimen/list_title_text"
            tools:ignore="HardcodedText" />

        <ImageView
            android:id="@+id/imageView1"
            android:contentDescription="@string/content_description"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/card_bg_pressed_top" />

    </RelativeLayout>

</RelativeLayout>

【问题讨论】:

    标签: android android-layout scrollview


    【解决方案1】:

    您可以使用android:descendantFocusability 属性来控制ScrollView 的后代是否应该获得焦点。如果您希望ScrollView 不自动向下滚动,请将android:descendantFocusability 属性的值设置为blocksDescendants,并将此属性添加到LinearLayout,它是ScrollView 的子级。

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:orientation="vertical"
        android:descendantFocusability="blocksDescendants" >
    

    【讨论】:

      【解决方案2】:

      要获得顶部视图的焦点,您可以在您的 java 文件中添加此代码....

      scrollview.fullScroll(View.FOCUS_UP);
      

      【讨论】:

        猜你喜欢
        • 2010-12-04
        • 1970-01-01
        • 1970-01-01
        • 2013-01-04
        • 2017-10-15
        • 2012-09-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多