2013-07-11 6 views
0

행의 오른쪽에 Buttons B1과 B2가 있고 그 행의 나머지 공간의 가운데에 Button A1과 A2가 있습니다.레이아웃 중복 피하기

Centering within remainder

이 포장 버튼 두 번없이 수 있습니까?

여기에 구체적인 질문이 있습니다.

Eclipse는 두 개의 상위 레이아웃을 @id/buttonAx으로 비평합니다. "이 LinearLayout 레이아웃 또는 RelativeLayout 부모는 아마도 쓸모가 없습니다."라고 비평합니다.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:id="@+id/myRightMostLL" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" > 

     <Button 
      android:id="@+id/buttonB1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="B1" /> 

     <Button 
      android:id="@+id/buttonB2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="B2" /> 

    </LinearLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@id/myRightMostLL"> 

     <LinearLayout 
      android:id="@+id/remainderLL" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true"> 

      <Button 
       android:id="@+id/buttonA1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="A1" /> 

      <Button 
       android:id="@+id/buttonA2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="A2" /> 

     </LinearLayout> 

    </RelativeLayout> 

</RelativeLayout> 
아직 버튼 B를 배치 한 후에 남아있는 공간에 버튼 A를 가운데 놓을 수 있어야합니다. 버튼 A에 대한 하나의 상위 래퍼 레이아웃으로 동일한 효과를 얻을 수 있습니까?

이와 같은 레이아웃에서도 @ id/myRightMostLL이 첫 번째로 인 파일에 표시되어야하는지 여부에 대해 자주 질문합니다.

+0

버튼 A의 왼쪽 여백과 오른쪽 여백을 설정하는 것 외에 다른 해결책을 생각할 수는 없지만 그 방법은 다양 할 것이라고 생각하지 않습니다. 솔직히 원하는 결과를 얻는다면 나는 단지 당신이 필요로하는 것을 이해하지 못하기 때문에 그 경고를 그냥 억제 할 것입니다. – user2483079

+0

추가 버튼이 ButtonA, ButtonB 또는 일부 그룹으로 그룹화됩니까? 어떻게 배치 될 것입니까? 두 개의 버튼으로 만 구성된 솔루션이 효과가 없을 것이라는 모호한 경고 대신 실제 요구 사항을 알려주는 것이 좋습니다. –

+0

@TedHopp Done .. – Calaf

답변

1

을 표시하지 않습니다 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <View 
     android:layout_width="0" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 
    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="A1" /> 
    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="A2" /> 
    <View 
     android:layout_width="0" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 
    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="A3" /> 
    <Button 
     android:id="@+id/button4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="A4" /> 
</LinearLayout> 

당신은 API (14)을 대상으로하는 경우 또는 나중에, 당신은 Space 위젯 대신 위의 View를 사용할 수 있습니다. SpaceView에 대한 경량 대안으로, 이러한 종류의 작업을 위해 특별히 설계되었습니다.

+0

인상적입니다. 따라서 layout_weight는 일종의 스프링 역할을합니다. 해당 속성을 보유하고있는보기가 인접 또는 분할 (전체 크기 합계와 같이) 될 필요가 없습니다. – Calaf

+0

@Calaf - 아니요. 그냥 빈보기가 모든 여분의 공간을 먹게하십시오. –

1

솔루션은 아래의 공간을 차지하기 위해 "필러"전망의 몇 가지를 사용하여, 당신은 하나의 레이아웃이 할 수 있다고 생각 아마도 쓸모없는 경고 :

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <RelativeLayout 
     android:layout_toLeftOf="@+id/layRight" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     > 
       <Button 
        android:id="@+id/button3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_toRightOf="@+id/button4" 
        android:text="Button" /> 

      <Button 
       android:id="@+id/button4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Button" /> 

    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/layRight" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" > 

       <Button 
        android:id="@+id/button1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_toRightOf="@+id/button2" 
        android:text="Button" /> 

      <Button 
       android:id="@+id/button2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Button" /> 

    </RelativeLayout> 


</RelativeLayout> 

http://clip2net.com/s/5nna7M

+0

정확히 4 대 버튼의 경우까지 확장하는 방법을 모르기 때문에 그럴 필요가 없습니다. 하지만 어쩌면 그렇게 할 수 있습니다. 당신은 (지금 확장 된) 질문을보고 당신의 대답이 그것에 적응 될 수 있는지 보시겠습니까? – Calaf

+0

곧 수정 사항을 제공 할 것입니다. –

+0

위의 편집 된 레이아웃. –