facebook twitter hatena line email

「Android/レイアウト/Layoutなどの使い方」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(参考)
行43: 行43:
 
す。
 
す。
  
 +
==レイアウトをコードで実行==
 +
activity_main.xml
 +
<pre>
 +
<LinearLayout
 +
        android:layout_width="match_parent"
 +
        android:layout_height="match_parent"
 +
        android:id="@+id/ll"
 +
        android:orientation="vertical">
 +
</pre>
 +
MainActivity.java
 +
<pre>
 +
LinearLayout ll = findViewById(R.id.ll);
 +
ll.setOrientation(LinearLayout.HORIZONTAL);
 +
TextView text = new TextView(getApplicationContext());
 +
text.setText("1111");
 +
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
 +
        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
 +
params.weight = 1.0f;
 +
text.setLayoutParams(params);
 +
ll.addView(text);
 +
TextView text2 = new TextView(getApplicationContext());
 +
text2.setText("2222");
 +
text2.setLayoutParams(params);
 +
ll.addView(text2);
 +
TextView text3 = new TextView(getApplicationContext());
 +
text3.setText("3333");
 +
text3.setLayoutParams(params);
 +
ll.addView(text3);
 +
</pre>
  
 
==ConstraintLayout==
 
==ConstraintLayout==

2019年8月21日 (水) 15:32時点における版

レイアウト種類

  • LinearLayout ・・レイアウトを横に並べる
  • FrameLayout ・・レイアウトを重ねる
  • RelativeLayout ・・上の2つの組み合わせを同時にできる

水平一直線

<LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
   >

垂直一直線

<LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical"
   >

絶対位置を指定

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"


Linearでの配置

android:gravity="right"
android:gravity=”bottom”
top  コンテナの上部に配置
bottom  コンテナの下部に配置
right  コンテナの左側に配置
left  コンテナの右側に配置
fill  オブジェクトの高さ・幅を、コンテナのサイズに合わせる。
fill_vertical  オブジェクトの高さをコンテナのサイズに合わせる。
fill_horizontal  オブジェクトの幅をコンテナのサイズに合わせる。
center  オブジェクトを中央に配置する。サイズ変更無し。
center_vertical  オブジェクトを上下中央に配置しする。サイズ変更無し。
center_horizontal  オブジェクトを左右中央に配置する。サイズ変更無し。
clip_vertical  top/bottomの追加オプションとして、オブジェクトの上部/下部の境界をコンテナの境界に合わせます。
clip_horizontal  left/rightの追加オプションとして、オブジェクトの左側/右側の境界をコンテナの境界に合わせま

す。

レイアウトをコードで実行

activity_main.xml

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/ll"
        android:orientation="vertical">

MainActivity.java

LinearLayout ll = findViewById(R.id.ll);
ll.setOrientation(LinearLayout.HORIZONTAL);
TextView text = new TextView(getApplicationContext());
text.setText("1111");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.weight = 1.0f;
text.setLayoutParams(params);
ll.addView(text);
TextView text2 = new TextView(getApplicationContext());
text2.setText("2222");
text2.setLayoutParams(params);
ll.addView(text2);
TextView text3 = new TextView(getApplicationContext());
text3.setText("3333");
text3.setLayoutParams(params);
ll.addView(text3);

ConstraintLayout

Android Studio 2.2 previewよりConstraintLayoutが追加された

https://qiita.com/tomoima525/items/0584be581a0d3a4db8c5

参考

右上とか左下とか9分割とかの参考ページ

http://d.hatena.ne.jp/Korsakov/20110131/1296504108