2013-04-24 6 views

Odpowiedz

3

można dostosować za pomocą układu liniowego przy użyciu wagi Wkleiłem przykładowy kod poniżej nadzieję, że to pomaga.

<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

Utwórz niestandardowy widok, który rozszerza klasę View i zastępuje metodę onDraw(), aby utworzyć żądany prostokąt. możesz zapoznać się z: Android canvas draw rectangle, aby uzyskać ogólny pomysł.

Jeśli pytanie brzmi: Jak ustawić widok wewnątrz kontenera - dodać to w konstruktorze widoku macierzystego:

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

      }  
     } 
    }); 

gdzie instance jest nawiązaniem do widoku kontenera.

3

Tak. Jest to możliwe przy użyciu rodzica układu Względny układ i innego układu (twojego pola) w środku, które jest ustawione centralnie. a szerokość i wysokość twojego pudełka może być wymieniona w java, a nie w xml.