2013-04-24 28 views

Trả lời

3

bạn có thể điều chỉnh nó bằng cách sử dụng bố cục tuyến tính sử dụng trọng số tôi đã dán mã mẫu bên dưới hy vọng điều này sẽ hữu ích.

<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

Tạo chế độ xem tùy chỉnh mở rộng lớp View và ghi đè phương thức onDraw() để tạo hình chữ nhật mong muốn. bạn có thể tham khảo: Android canvas draw rectangle để có ý tưởng chung.

Nếu câu hỏi của bạn là: Làm thế nào để xác định vị trí một cái nhìn bên trong một container - thêm này trong constructor của cái nhìn mẹ:

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

      }  
     } 
    }); 

nơi instance là một tài liệu tham khảo để xem container của bạn.

3

Vâng. Điều này là có thể bằng cách sử dụng bố mẹ tương đối bố trí và bố cục khác (hộp của bạn) bên trong vị trí đó làm trung tâm. và chiều rộng và chiều cao của hộp của bạn có thể được đề cập trong java, chứ không phải trong xml.

Các vấn đề liên quan