android - 为什么我的 ScrollView 只有在不需要滚动的时候才会太宽?

标签 android android-layout scrollview

我有一个 Activity 布局文件:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/compose_message_view"
        style="@style/Container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout style="@style/SectionContainer" >

            <TextView
                style="@style/FieldHeader"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawablePadding="8dp"
                android:focusable="true"
                android:focusableInTouchMode="true" />

            <Spinner
                android:id="@+id/compose_message_department"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="8dp"
                android:prompt="@string/compose_message_department_prompt"
                android:spinnerMode="dropdown" />
        </LinearLayout>

        <LinearLayout style="@style/SectionContainer" >

            <TextView
                style="@style/FieldHeader"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawablePadding="8dp"
                android:text="@string/message_account_label" />

            <Spinner
                android:id="@+id/compose_message_account"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="8dp"
                android:prompt="@string/compose_message_account_prompt"
                android:spinnerMode="dropdown" />
        </LinearLayout>

        <EditText
            android:id="@+id/compose_message_subject"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="32dp"
            android:layout_marginTop="16dp"
            android:hint="@string/compose_message_subject_hint"
            android:inputType="textCapSentences" />

        <EditText
            android:id="@+id/compose_message_body"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/compose_message_body_hint"
            android:inputType="textMultiLine|textCapSentences" />
    </LinearLayout>

</ScrollView>

相关样式如下所示:

<style name="Container">
    <item name="android:layout_marginRight">130dp</item>
    <item name="android:layout_marginLeft">130dp</item>
    <item name="android:paddingTop">32dp</item>
    <item name="android:paddingLeft">32dp</item>
    <item name="android:paddingRight">32dp</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:orientation">vertical</item>
</style>

<style name="SectionContainer">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:orientation">vertical</item>
    <item name="android:paddingBottom">16dp</item>
</style>

<style name="FieldHeader" parent="android:Widget.TextView">
    <item name="android:textAllCaps">true</item>
    <item name="android:paddingLeft">12dp</item>
    <item name="android:paddingTop">24dp</item>
    <item name="android:paddingRight">12dp</item>
</style>

现在,当我在 Nexus 7 上横向打开此 Activity(尚未尝试过其他平板电脑)时,ScrollView 及其内容超出了屏幕的右边缘。当我在 compose_message_body EditText 中键入几行以使 UI 延伸到屏幕底部下方时,UI 的右边缘进入它所属的位置。请参阅下面的屏幕截图。

LinearLayout is short and does not cause ScrollView to scroll - for some reason it's too wide LinearLayout is long and causes ScrollView to scroll - and now it has perfect width

关于这里发生的事情有什么想法吗?

最佳答案

这是在 ScrollView 上使用 fillViewPort 属性的副作用。如果您已将 fillViewPort 设置为 true,则仅当您的 child 的测量高度低于您的 ScrollView 的高度时才会考虑这一点。在这种情况下,ScrollView 将拉伸(stretch)内容以填充视口(viewport),并且您的边距将无法正确应用。一旦您 child 的内容大于您的 ScrollView 的高度,您的边距就会被正确应用。

我建议不要在您放置在 ScrollView 中的子布局 (LinearLayout) 上使用边距,而只使用填充。

所以你的 Container 风格可以变成

<style name="Container">
    <item name="android:paddingTop">32dp</item>
    <item name="android:paddingLeft">164dp</item>
    <item name="android:paddingRight">164dp</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:orientation">vertical</item>
</style>

关于android - 为什么我的 ScrollView 只有在不需要滚动的时候才会太宽?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23367038/

相关文章:

android - ImageView 很慢

android - 如何在 Android 中从一种布局切换到另一种布局

android - 在信任库中添加自定义证书

Android 持久 Bottom Sheet 初始可见性

android - android listView 行向左或向右对齐

Android onClickListener 在复合 View 中不起作用

android - 最小日期最大日期在 xamarin android 中不起作用

android - 如何在android studio中进行全新安装

scrollview - react native : 2 scroll views with 2 sticky headers

ios - 属性 'scrollView' 需要定义方法 '-scrollView' - 使用@synthesize、@dynamic 或提供方法实现?