0
0

More than 1 year has passed since last update.

【Android】webviewにアプリで作ったヘッダーを入れ込みたい

Posted at

やりたいこと

こんな感じで、webviewとアプリ側のヘッダーを連結させる画面を作成しようとしていました。
今までLinearLayoutでヘッダーを上部に固定し、その下にwebviewを配置していたので、このような実装ができるか調べてみた次第です。
(紫のScrollable Headerがアプリ側で作成したヘッダー。その下のGoogleのページがwebview)
タイトルなし.gif

解決策

ScrollViewを使うことでめちゃくちゃ簡単に解決しました。

Sample.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">

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.AppCompatTextView
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:background="@color/purple_200"
            android:text="Scrollable Header"
            android:textSize="32sp" />

        <WebView
            android:id="@+id/top_webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </androidx.appcompat.widget.LinearLayoutCompat>
</ScrollView>

注意点

ScrollViewは子となるViewGroupを1つしか持てないので、LinearLayoutなりを配置しましょう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0