2010年11月26日金曜日

Android 高さが横と同じになるカスタム ViewGroup を作ってみた。

こんなにまじめに View.java と ViewGroup.java と FrameLayout.java を読んだのは初めてだわ。勉強になった。getSuggestedMinimumHeight () とか、いろいろ試して結局こうなったんだけど。すっごい短いよ。
ほんとこれだけ。




  1. package yanzm.example.squarelayoutsample;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.widget.FrameLayout;  
  6.   
  7. public class SquareLayout extends FrameLayout {  
  8.   
  9.    
  10.     public SquareLayout(Context context) {  
  11.         super(context);  
  12.     }  
  13.    
  14.     public SquareLayout(Context context, AttributeSet attrs) {  
  15.         super(context, attrs);  
  16.     }  
  17.   
  18.    
  19.     @Override  
  20.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  21.         super.onMeasure(widthMeasureSpec, widthMeasureSpec);  
  22.     }    
  23. }  


  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <LinearLayout  
  8.         android:orientation="horizontal"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"  
  11.         >  
  12.     <Button    
  13.         android:layout_width="wrap_content"   
  14.         android:layout_height="wrap_content"   
  15.         android:text="button"  
  16.         />  
  17.     <yanzm.example.squarelayoutsample.SquareLayout  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="fill_parent"  
  20.         android:background="@android:color/white"  
  21.         android:layout_weight="1"  
  22.         >  
  23.         <Button  
  24.             android:layout_width="fill_parent"   
  25.             android:layout_height="fill_parent"   
  26.             android:text="button"  
  27.             />  
  28.     </yanzm.example.squarelayoutsample.SquareLayout>  
  29.     <Button  
  30.         android:layout_width="wrap_content"   
  31.         android:layout_height="wrap_content"   
  32.         android:text="button"  
  33.         />  
  34.     </LinearLayout>  
  35. </LinearLayout>  



ちなみに、中のボタンは表示されないけどバックグラウンドはADTのレイアウトエディタでも表示されたよ。




 

0 件のコメント:

コメントを投稿