385

LinearLayout内部に があるaScrollViewがありますがandroid:layout_height="fill_parent"、 の高さいっぱいまで拡張しませんScrollView。私のレイアウトは次のようになります。

level    layout    layout_width    layout_height
1    LinearLayout    fill_parent    fill_parent
2    LinearLayout    fill_parent    wrap_content
3    (some irrelevant stuff)
2    ScrollView      fill_parent    fill_parent <-- this expands full height
3    LinearLayout    fill_parent    fill_parent <-- this does not (has orientation=vertical)
(following stuff probably are irrelevant, but just to be sure:)
4    TextView        fill_parent    fill_parent
4    LinearLayout    fill_parent    wrap_content

のEclipse でを選択すると([アウトライン] パネルで) がLinearLayoutの高さ全体に拡張されないことがわかります画面の下部まで拡大しません。どうすればそうすることができますか?ScrollViewAndroid Layout EditorScrollViewLinearLayout

私が達成しようとしている効果は、テキストとその下にボタンを配置することです (LinearLayoutレベル 4 の内部にはボタンしかありません)。テキストは、スクロールバーが必要なほど大きくなる可能性があります。その場合、ユーザーはボタンを表示するために下にスクロールする必要があります。テキストがスクロール バーに対して十分な大きさでない場合はLinearLayout、ボタンを含むものを画面の下部に貼り付けます。

最初、私は完全な XML を投稿するべきではないと考えていました。なぜなら、通常、質問に膨大な量のコードが含まれているのを見るのはターンダウンになるからです。ただし、必要になる可能性があるため、完全なレイアウトを次に示します。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/video_layout"
        android:focusable="true"
        style="@style/VideoLayout">
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:foreground="@android:drawable/ic_media_play"
            android:foregroundGravity="center">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/video_thumb"
                android:padding="5dip"
                android:background="#454545"/>
        </FrameLayout>
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:focusable="true"
            style="@style/VideoTitle"
            android:id="@+id/video_title"
            android:layout_gravity="center"
            android:layout_weight="1"/>
    </LinearLayout>
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
        <!-- this ScrollView expands the full height of the screen.
             However, the next LinearLayout does not expand the full height of the ScrollView -->
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:layout_gravity="fill"
            android:orientation="vertical"
            android:id="@+id/info_layout">
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/info_body"
                style="@style/InfoText"/>
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                style="@android:style/ButtonBar">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_weight="1"
                        android:text="@string/button_readmore"
                        android:id="@+id/btn_read_more"/>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

現時点ではandroid:layout_gravity="bottom"、問題のあるに頼ってLinearLayoutいます。これにより、ボタンが画面の下部に固定されます。しかし、それはまた、テキストが画面の下部にくっつくようになります。これは、私が求めていたものとはまったく異なります.

更新:それをスクラッチするandroid:layout_gravity="bottom"ScrollView、スクロールできなくなります。他のアイデア?

4

7 に答える 7

992

最後に自分で解決策を見つけました。問題は ではなく にありLinearLayoutました( が拡大していたのに対し、拡大していなかっScrollViewたという事実を考えると、奇妙に思えます)。ScrollView LinearLayout

解決策は、 で使用することでしandroid:fillViewport="true"ScrollView

于 2010-04-09T10:29:10.843 に答える
24

android:fillViewport="true"この投稿が非常に古いことは知っています。キーボードの上に編集テキストが表示されないことがあるため、使用したくない人のために。LinearLayout の代わりに Relative レイアウトを使用すると、目的が解決します。

于 2015-09-14T07:12:57.197 に答える
10

レイアウト xml を提供できますか? そうすることで、人々は最小限の労力で問題を再現できるようになります。

設定する必要があるかもしれません

android:layout_weight="1"

拡張したいアイテムの

于 2010-04-08T13:09:53.370 に答える
2

私はこれを取得するために動的に使用しました。私はLinearLayoutを持っており、この中で子としてScrollViewを使用しました。次に、LinearLayoutを再度取得し、必要なビューをこのLinearLayoutに追加してから、このLinearLayoutをScrollViewに追加し、最後にこのScrollViewをLinearLayoutに追加します。次に、uはur ScrollViewでスクロールを取得でき、何も表示されません。

LinearLayout(Parent)--ScrollView(LinerLayoutの子)--LinearLayout(ScrollViewの子)-ここにtextView、Buttons、spinnerなどを追加します。次に、このLinearLyoutをScrollViewに追加します。BcozはScrollViewに1つのCHILDのみを適用し、最後にこのScrollViewをLinearLyoutに追加します。定義された領域が画面サイズを超えている場合、uはScrollView内にスクロールを取得します。

于 2010-12-09T18:58:44.373 に答える
1

私の場合、私は与えていません

LinearLayout(ScrollView's Child) の向き

したがって、デフォルトでは水平にスクロールしますが、scrollview は垂直にスクロールするため、'android:orientation="vertical"' が ScrollView の Child (LinearLayout の場合) に設定されているかどうかを確認してください。

これは私のケースでした

.

于 2015-02-03T07:47:30.860 に答える
1

ここでのすべての答えは(完全に)うまくいきませんでした。完全な答えを得るためにやりたいことを要約すると、おそらくデバイスのビューポートを埋める ScrollView があるため、レイアウト xml で fillViewport を「true」に設定します。次に、ScrollView 内に、他のすべてを含む LinearLayout があり、その LinearLayout は、少なくともその親 ScrollView と同じ高さにする必要があるため、(LinearLayout の) 一番下にあるはずのものは、実際には、私たちが望むように、画面の下部 (または、LinearLayout のコンテンツの高さが画面よりも高い場合は、ScrollView の下部)。

activity_main.xml レイアウトの例:

<ScrollView
    android:id="@+id/layout_scrollwrapper"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:layout_alignParentTop="true"
    android:layout_above="@+id/layout_footer"
    >
<LinearLayout
    android:id="@+id/layout_scrollwrapper_inner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
...content which might or might not be higher than screen height...
</LinearLayout>
</ScrollView>

次に、アクティビティの onCreate で、LinearLayout のレイアウトが完了するのを「待機」し (親のレイアウトも既に完了していることを意味します)、最小の高さを ScrollView の高さに設定します。したがって、ScrollView が画面の高さ全体を占めていない場合にも機能します。ScrollViewを呼び出す.post(...)か、内部の LinearLayout を呼び出すかは、それほど大きな違いはありません。一方がうまくいかない場合は、もう一方を試してください。

public class MainActivity extends AppCompatActivity {

@Override // Activity
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LinearLayout linearLayoutWrapper = findViewById(R.id.layout_scrollwrapper_inner);

    ...

    linearLayoutWrapper.post(() -> {
        linearLayoutWrapper.setMinimumHeight(((ScrollView)linearLayoutWrapper.getParent()).getHeight());
    });
}
...
}

悲しいことに、これは xml のみのソリューションではありませんが、私にとっては十分に機能します。この問題の解決策を求めてインターウェブを精査している他の拷問された Android 開発者にも役立つことを願っています;D

于 2020-07-06T08:54:05.650 に答える