かけるヒトからできるヒト

プログラムを書ける人からプログラムが出来る人へなるために個人的にまとめるブログ

androidのviewに隙間ができる

WebViewを使ったアプリを作っている途中、どうしても上下左右に隙間が出来て、それ以上viewを大きくできないなぁと思ったので備忘録。

 

課題

viewを配置した時の上下左右の隙間を無くす

 

原因

xmlでpadding(内側の隙間のサイズ)が決められてただけ。

 

解決

activity_main.xmlを見てみると、@dimen/activity_vertical_marginと@dimen/activity_horizontal_marginというのが設定されていた。

resフォルダにあるvalues/dimens.xmlの中のそれぞれのmarginの値を0にする事で、画面いっぱいいっぱいまでwebviewを広げる事ができた。

 

activity_main.xml

<LinearLayout 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="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <WebView
        android:id="@+id/mainWebView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

dimens.xml

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">0dp</dimen>
    <dimen name="activity_vertical_margin">0dp</dimen>
</resources>