【Android】Viewの上下だけにボーダー(境界線)を追加する方法
この記事では、AndroidのView(TextViewやButtonなど)の上部と下部にのみボーダー(境界線)を表示させる方法を、実際のコード例とともに段階的に解説します。
左右のボーダーは不要で「上下だけ線を引きたい」というケースはリスト区切りやカードデザインなどでよく登場します。ポイントは、<layer-list>を使ったdrawableの重ね合わせテクニックです。
実装手順
ステップ1:新規プロジェクトの作成
まずAndroid Studioで新しいプロジェクトを作成します。メニューから「File」→「New Project」を選択し、必要な項目を入力してプロジェクトを作成してください。
ステップ2:ボーダー用のdrawableを作成する
res/drawable/border_top_bottom.xml を新規作成し、以下のコードを記述します。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#e10606" />
<solid android:color="#9bce64" />
</shape>
</item>
<item android:bottom="2dp" android:top="2dp">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#9bce64" />
</shape>
</item>
</layer-list>このコードの仕組みは次のとおりです。
- 1つ目の
<item>で、四辺すべてに赤いストローク(2dp)を持つ矩形を描画します。 - 2つ目の
<item>で、上下に2dpずつの余白を設けた背景色(#9bce64)の矩形を上に重ねます。
この重ね合わせにより、中央部分が背景色で覆われ、結果として上下のボーダーだけが見える状態になります。
ステップ3:レイアウトファイルの作成
res/layout/top_bottom_border_in_android_view.xml を作成し、以下のコードを記述します。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Adding Border in Top and Bottom of an Android View" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@drawable/border_top_bottom"
android:padding="30dp"
android:text="Top Bottom Border in TextView"
android:textColor="#000000"
android:textSize="18sp" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@drawable/border_top_bottom"
android:text="Top Bottom Border in Button" />
</LinearLayout>ここでは、作成したdrawableをandroid:background="@drawable/border_top_bottom"としてTextViewとButtonに適用しています。
ステップ4:strings.xmlの編集
res/values/strings.xml にアプリ名を定義します。
<resources>
<string name="app_name">Adding Border to an Android View</string>
</resources>ステップ5:activity_main.xmlの確認
res/layout/activity_main.xml はプロジェクト作成時のデフォルトのままでも動作しますが、参考として以下のコードになっています。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>なお、本サンプルでは次のステップでsetContentView()に独自レイアウトを直接指定するため、このactivity_main.xmlは画面には使用されません。
ステップ6:MainActivity.javaの実装
src/MainActivity.java に以下のコードを記述します。
package com.example.sample1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.top_bottom_border_in_android_view);
}
}setContentView()で、ステップ3で作成したレイアウトを指定している点に注目してください。
ステップ7:AndroidManifest.xmlの確認
Manifests/AndroidManifest.xml は以下のようになります。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:dist="https://schemas.android.com/apk/distribution"
package="com.example.sample1">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<dist:module dist:instant="true" />
</manifest>アプリを実行してみよう
それではアプリを実行しましょう。ここでは実機のAndroidスマートフォンをPCに接続しているものとします。Android Studioでプロジェクト内のアクティビティファイルを開き、ツールバーの実行(Run)アイコンをクリックしてください。接続中のモバイルデバイスを選択すると、端末に以下のような画面が表示されます。

TextViewとButtonの上下にのみ赤いボーダーが描画されていれば成功です。layer-listのオフセット値(android:top/android:bottom)やストロークの色・太さを変更すれば、好みのデザインに簡単に調整できます。
-
Androidで円形のImageViewに影と境界線を追加する方法【コード例付き】
はじめにこの記事では、AndroidアプリのImageViewに表示する画像を円形に切り取り、さらに白い境界線と影(外枠)を追加する方法を、実際のコード例とともに段階的に解説します。ポイントは、RoundedBitmapDrawableFactoryで生成できるRoundedBitmapDrawableを使って画像を円形化し、PaintとCanvasを使って境界線や影を描画するところです。手順1:新規プロジェクトを作成するAndroid Studioを開き、メニューから「File」→「New Project」を選択して、必要な項目を入力し新しいプロジェクトを作成します。今回は空のアクティビティ
-
Androidでページの最上部・最下部へ一瞬でジャンプする方法【rootアプリ&ブラウザ別テクニック】
長いページのスクロールが面倒な問題 ウェブサイトで本当に見たいコンテンツにたどり着くまで、何度も何度もスクロールしなければならないとき、誰もがイライラした経験があるはずです。ページの上部で見つけた情報をもう一度確認したいのに、今はページの最下部にいて、指を何度も動かして戻らなければならない——そんな場面は珍しくありません。急いでいるときには、この作業がさらに苦痛になります。こうした場合、どうすればよいのでしょうか。 幸い、「OneClick Scroll」というAndroidアプリを使えば、端末上のあらゆるページの最上部や最下部へワンタップでジャンプできます。ここではその使い方を詳しく解説し