【Android】LinearLayoutで余白の部分に100%でViewを配置する

【Android】LinearLayoutで余白の部分に100%でViewを配置する

2015-09-04

Androidで一部分を固定幅で表示し、余白部分に最大幅でViewを配置したいときには親のViewには

「android:weightSum」を指定せずに余白いっぱいに表示させたい子ビューのパラメータに

「android:layout_weight=”1″」と指定してやると余白部分をうまく使って描画してくれる

もしパーセント指定したかったらサポートライブラリの「PercentRelativeLayout」を使ったほうがいいかも・・・。

サンプル

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
android:orientation="horizontal"
    android:layout_height="match_parent">

<Button
        android:id="@+id/sample_btn"
        android:layout_width="120dp"
        android:layout_height="60dp"
        android:text="固定サイズ" />


<TextView
        android:id="@+id/sample_text_view"
        android:layout_width="0dp"
        android:layout_height="60dp"
android:layout_weight="1"
        android:text="余った部分" />

<LinearLayout>