2013-04-24 71 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來獲得大致的想法。

如果你的問題是:如何放置在容器內視圖 - 在父視圖的構造函數中添加此:

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); 

      }  
     } 
    }); 

其中instance是你的容器視圖的參考。

3

是的。這可以使用一個相對佈局父母和另一個佈局(你的盒子)作爲中心。而你的盒子的寬度和高度可以在java中提到,而不是在xml中提到。