LoginSignup
4
5

More than 5 years have passed since last update.

Android レイアウトで直線を描画する方法

Posted at

要旨

Android のビュー上に、分割線やグリッドラインのような、画面上の固定位置に直線を引くのは、Viewを使い、「1dpの幅」だと垂直線、「1dpの高さ」だと水平線、とやるのが便利です。

以下、画面中央に垂直線・水平線を引くレイアウト設定です。

layout/activity_main.xml```
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    <View
            android:layout_width="wrap_content"
            android:layout_height="1dp"
            android:id="@+id/line_horizontal_center"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"         
            app:layout_constraintEnd_toEndOf="parent"
            android:visibility="visible"
            android:background="@color/colorAccent"/>
    <View
            android:layout_width="1dp"
            android:layout_height="wrap_content"
            android:id="@+id/line_vertical_center"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
            android:background="@color/colorPrimaryDark"/>
</android.support.constraint.ConstraintLayout>

スクリーンショットです。
Screenshot_20190322-004107.png

参考資料

本投稿の元ネタです。この動画の投稿元の”CodingDemo" も役に立ちそうです。

Android のレイアウト要素の配置を決めるための制約 "constraintLayout" について詳しく説明しています。

4
5
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
4
5