2013-07-08 40 views
7

Nhiệm vụ rất đơn giản: có hai nút và TextView phía trên chúng. Tất cả các tiện ích sẽ được căn giữa trong bố cục tương đối. Ý tưởng duy nhất tôi có là tạo widget thứ ba View và sử dụng nó làm trục trung tâm cho các nút. Bất kỳ ý tưởng? Bố cục dự phòng không phải là giải pháp tốt.Làm cách nào để căn giữa hai chế độ xem trong bố cục tương đối?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/tv_progress" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="@string/app_name" /> 

    <View 
     android:id="@+id/view_axis" 
     android:layout_width="1dp" 
     android:layout_height="1dp" 
     android:layout_below="@id/tv_progress" 
     android:layout_centerInParent="true" /> 

    <Button 
     android:id="@+id/button_start" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/tv_progress" 
     android:layout_toLeftOf="@id/view_axis" 
     android:text="@string/start" /> 

    <Button 
     android:id="@+id/button_stop" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/tv_progress" 
     android:layout_toRightOf="@id/view_axis" 
     android:text="@string/stop" /> 

</RelativeLayout> 
+0

Nhìn đây, này trả lời câu hỏi, câu trả lời đánh dấu là không phải những gì ban đầu được hỏi. http://stackoverflow.com/questions/10904864/relativelayout-gravity-center-not-working/13280255#13280255 – user1806772

Trả lời

5

Nếu tôi hiểu những gì bạn muốn một cách chính xác, bạn có thể đặt Button s trong một LinearLayout và trung tâm mà

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<TextView 
    android:id="@+id/tv_progress" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:text="@string/app_name" /> 
<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_below="@id/tv_progress"> 
<Button 
    android:id="@+id/button_start" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/start" /> 

<Button 
    android:id="@+id/button_stop" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/stop" /> 
</LinearLayout> 

Tôi không chắc chắn nếu đó là những gì bạn có nghĩa là bởi một "bố trí dự phòng" nhưng làm điều này là tốt nếu nó mang lại cho bạn những gì bạn muốn.

+1

chắc chắn, tôi có thể. Nhưng đó là một 'LinearLayout' dư thừa. Sau khi tôi đã đọc [thủ thuật bố cục Android # 3] (http://android-developers.blogspot.ru/2009/03/android-layout-tricks-3-optimize-by.html) và [Thủ thuật bố cục Android # 1] (http://android-developers.blogspot.ru/2009/02/android-layout-tricks-1.html), tôi bắt đầu xây dựng bố cục cẩn thận hơn. –

+2

Sử dụng nó theo cách này sẽ không làm bạn tổn thương. Có lẽ nếu nó trong một 'ListView' nhưng nếu điều này không phải là một vấn đề. Bạn không muốn quá nhiều 'bố cục' lồng nhau không cần thiết nhưng trong trường hợp này, đó là cách dễ nhất tôi thấy để làm điều đó. Vì vậy, nó không phải là không cần thiết và chỉ có một lồng nhau 'bố trí ' – codeMagic

+0

cảm ơn! Tôi đồng ý, 'LinearLayout' lồng nhau là OK trong trường hợp này. –

0

này theo chiều dọc và theo chiều ngang sẽ tập trung toàn bộ khối bao gồm các TextView + nút

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_centerInParent="true"> 

     <TextView 
      android:id="@+id/tv_progress" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/app_name" /> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <Button 
       android:id="@+id/button_start" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/start" /> 

      <Button 
       android:id="@+id/button_stop" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/stop" />  

     </LinearLayout> 

    </LinearLayout> 

</RelativeLayout> 
+1

Bạn đang thêm một 'LinearLayout' không cần thiết theo cách này – codeMagic

+0

Làm cách nào để bạn căn giữa toàn bộ khối? Với cách của bạn, TextView sẽ được căn giữa và các nút sẽ xuất hiện bên dưới chúng. –

+0

'Nút' sẽ xuất hiện ở giữa bên dưới 'TextView' được căn giữa. Tôi nghĩ rằng đây là những gì OP là đi cho nhưng có thể là sai. Tôi chưa có cơ hội so sánh hai 'layout' nhưng chúng sẽ có cùng tác dụng – codeMagic

2
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <Spinner 
     android:id="@+id/sp_rooms" 
     android:layout_toLeftOf="@id/space" 
     android:layout_centerVertical="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <Space 
     android:id="@+id/space" 
     android:layout_centerInParent="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/btn_registration" 
     android:layout_centerVertical="true" 
     android:layout_toRightOf="@id/space" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</RelativeLayout> 
Các vấn đề liên quan