2013-04-24 18 views

答えて

3

これは、体重を使った線形レイアウトを使って調整することができます。 これが役立つことを願って、以下のサンプルコードを貼り付けました。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:background="@color/transparent" 
> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_weight="1" 
android:orientation="horizontal" 
> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_weight="5" 
/> 
<TextView 
    android:id="@+id/desiredbox" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text="TextView" 
    android:layout_gravity="center" 
    android:background="@color/skyblueBackground" 
    android:layout_weight="1" 
    /> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_weight="5" 
/> 
</LinearLayout> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_weight="1" 
/> 

2

Viewクラスを拡張するカスタムビューを作成し、onDraw()メソッドをオーバーライドして目的の矩形を作成します。あなたは一般的な考えを得るために:Android canvas draw rectangleを参照することができます。あなたの質問がある場合は

:コンテナ内のビューを配置する方法 - 親ビューのコンストラクタでこれを追加します。instanceは、あなたのコンテナビューへの参照である

final ViewTreeObserver vto = this.getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

     @Override 
     public void onGlobalLayout() { 
       // here, the dimensions of the parent are available 
       int containerHeight = instance.getHeight(); 
       int containerWidth = instance.getWidth();  

       childView.setY(containerHeight/3); 
       childView.setX(containerWidth * 3/5); 
       instance.getViewTreeObserver().removeGlobalOnLayoutListener(this); 

      }  
     } 
    }); 

3

うん。これは、相対レイアウトの親と、その中心に配置された別のレイアウト(ボックス)を使用して行うことができます。あなたのボックスの幅と高さは、xmlではなくjavaで記述することができます。

関連する問題