2013-05-11 1 views
1

네 개의 단추가 꽉 조여서 정사각형을 이루고 있습니다. 중심에서 네 개의 단추 위에 단추를 배치하는 방법을 알아 내려고 노력하고 있습니다. 여기에 네 개의 버튼 내 현재 레이아웃은 다음과 같습니다테이블 레이아웃 위에 버튼을 레이어로 배치 할 수 있습니까?

<TableRow android:layout_weight="0.2" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal|center" 
     android:layout_span="2" 
     android:gravity="center" 
     android:text="TextView" 
     android:textColor="#357ae8" 
     android:textSize="20dp" 
     android:textStyle="bold" /> 
</TableRow> 

<TableRow 
    android:layout_gravity="center_horizontal" 
    android:layout_weight="1" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_column="0" 
     android:layout_marginBottom="-7dp" 
     android:layout_marginLeft="5dp" 
     android:layout_weight="1" 
     android:background="@drawable/b1tl" 
     android:gravity="center_horizontal" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_column="0" 
     android:layout_marginBottom="-7dp" 
     android:layout_marginRight="5dp" 
     android:layout_weight="1" 
     android:background="@drawable/b2tr" 
     android:gravity="center_horizontal" /> 
</TableRow> 

<TableRow 
    android:layout_margin="0dp" 
    android:layout_weight="1" > 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_column="0" 
     android:layout_marginLeft="5dp" 
     android:layout_marginTop="-7dp" 
     android:layout_weight="1" 
     android:background="@drawable/b3bl" 
     android:gravity="center_horizontal" /> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_column="0" 
     android:layout_marginRight="5dp" 
     android:layout_marginTop="-7dp" 
     android:layout_weight="1" 
     android:background="@drawable/b4br" 
     android:gravity="center_horizontal" /> 
</TableRow> 

<TableRow 
    android:layout_margin="10dp" 
    android:layout_weight="0.1" > 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_column="0" 
     android:layout_weight="1" 
     android:gravity="center_horizontal" 
     android:text="Lives" 
     android:textColor="#357ae8" /> 

    <TextView 
     android:id="@+id/textView5" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_column="0" 
     android:layout_weight="1" 
     android:gravity="center_horizontal" 
     android:text="Score" 
     android:textColor="#357ae8" /> 
</TableRow> 

<TableRow 
    android:layout_margin="10dp" 
    android:layout_weight="0.1" > 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_column="0" 
     android:layout_weight="1" 
     android:gravity="center_horizontal" 
     android:text="" 
     android:textColor="#357ae8" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_column="0" 
     android:layout_weight="1" 
     android:gravity="center_horizontal" 
     android:text="" 
     android:textColor="#357ae8" /> 
</TableRow> 

</TableLayout> 
나는 그것이의 제곱의 중심에 있었다 그래서 어쩌면 상대 레이아웃을 사용하고 버튼을 시도하고 하드 코드 수 생각

버튼을 사용했지만 시도하자마자 테이블 레이아웃에 영향을주었습니다. Android 레이아웃에서 이런 종류의 작업을 수행 할 수 있습니까?

+1

대신 'FrameLayout'을 사용해보십시오. –

+0

선형 레이아웃을 사용하고 방향을 수직으로 설정하고 테이블 앞에 버튼이 있습니다. – Raghunandan

답변

3

안녕하세요 FrameLayout을 사용하면 가능합니다. 다음은 버튼이 한 행의 두 버튼 사이에 중앙에 배치 된 완성 된 코드입니다. 마찬가지로 4 개의 버튼 모두에서 버튼을 중앙에 배치하여 해결할 수 있습니다. 여기에 내가 한 일이 있습니다 : 테이블 행에> FrameLayout. FrameLayout 내에서 LinearLayout 및 자식으로 Button을 사용하여 both can overlap (framelayout의 속성). 그렇게함으로써 우리가 LinearLayout 안에 위치하는 곳은 이전 버튼과 겹칠 것입니다.


이 코드를 확인하십시오. 나는 이것이 당신이 원하는 생각 :

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

    <TableLayout 
     android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

<TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="0.2" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal|center" 
     android:layout_span="2" 
     android:gravity="center" 
     android:text="TextView" 
     android:textColor="#357ae8" 
     android:textSize="20dp" 
     android:textStyle="bold" /> 
</TableRow> 

<TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center_horizontal" 
    android:layout_weight="1" > 

    <FrameLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" > 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="horizontal" 
       android:layout_weight="1" > 

       <Button 
        android:id="@+id/button1" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_marginBottom="-7dp" 
        android:layout_marginLeft="5dp" 
        android:layout_weight="1" 
        android:background="@drawable/ic_launcher" 
        android:gravity="center_horizontal" /> 

       <Button 
        android:id="@+id/button2" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_marginBottom="-7dp" 
        android:layout_marginRight="5dp" 
        android:layout_weight="1" 
        android:background="@drawable/ic_launcher" 
        android:gravity="center_horizontal" /> 
      </LinearLayout> 

      <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="horizontal" 
       android:layout_weight="1" > 

       <Button 
        android:id="@+id/button1" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_marginBottom="-7dp" 
        android:layout_marginLeft="5dp" 
        android:layout_weight="1" 
        android:background="@drawable/ic_launcher" 
        android:gravity="center_horizontal" /> 

       <Button 
        android:id="@+id/button2" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_marginBottom="-7dp" 
        android:layout_marginRight="5dp" 
        android:layout_weight="1" 
        android:background="@drawable/ic_launcher" 
        android:gravity="center_horizontal" /> 
      </LinearLayout> 

     </LinearLayout> 



     <Button 
      android:id="@+id/button5" 
      android:layout_width="wrap_content" 
      android:layout_height="150dp" 
      android:layout_gravity="center" 
      android:text="Center Button" /> 

    </FrameLayout> 

</TableRow> 


<TableRow 
    android:layout_margin="10dp" 
    android:layout_weight="0.1" > 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_column="0" 
     android:layout_weight="1" 
     android:gravity="center_horizontal" 
     android:text="Lives" 
     android:textColor="#357ae8" /> 

    <TextView 
     android:id="@+id/textView5" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_column="0" 
     android:layout_weight="1" 
     android:gravity="center_horizontal" 
     android:text="Score" 
     android:textColor="#357ae8" /> 
</TableRow> 

<TableRow 
    android:layout_margin="10dp" 
    android:layout_weight="0.1" > 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_column="0" 
     android:layout_weight="1" 
     android:gravity="center_horizontal" 
     android:text="" 
     android:textColor="#357ae8" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_column="0" 
     android:layout_weight="1" 
     android:gravity="center_horizontal" 
     android:text="" 
     android:textColor="#357ae8" /> 
</TableRow> 

</TableLayout> 

</RelativeLayout> 
+0

이것은 훌륭합니다, 지금 실험 중이지만 네 위치의 중심에 위치하면 위치에 영향을 미칩니다. 다른 버튼의 레이아웃, 어쩌면 나는 그것을 잘못하고있다, 나는 계속 노력할 것이다! 고맙습니다! :) – deucalion0

+0

레이아웃에 영향을주지 않고 코드를 네 개의 버튼 사이에 배치하려면 어떻게해야합니까? 감사! – deucalion0

+1

아직하지 않았다면 다음과 같이 생각할 수 있습니다. 연속으로 Framelayout과 버튼을 사용하여 둘 다 겹칩니다. framelayout 안에는 수평 선형 레이아웃이 있습니다. 수평 선형 레이아웃의 첫 번째 자식은 두 개의 자식이있는 또 다른 수직 선형 레이아웃입니다. Horizontal linera 레이아웃의 두 번째 자식은 두 개의 자식이있는 또 다른 수직 선형 레이아웃입니다. 따라서 이러한 요소의 가운데에 초기 단추를 배치 할 수 있습니다. 당신이 그것을 얻을 희망, 그렇지 않으면 알려주세요. – ruben

1
내가 모르는 뭔가가 될 수 있지만

... 당신이 그것을 상단으로 바닥을 정렬하는 RelativeLayout 안쪽에 현재 레이아웃을 넣을 경우 어떻게

는 & 좌우 .

변경 사항을 확인하고 여전히 올바르게 작동합니다.

그런 다음 RelativeLayout 안에 버튼을 배치하고이 가운데에 맞 춥니 다. RelativeLayout.

이클립스를 사용하여 편리하게 확인할 수는 없지만 그렇게 간단하지 않아야합니까?


매우 어떤 @durgomgiri는 his answer 말했다 즉.

+1

네가 맞다면, 그것이 작동하고 nikmin이 한 일이다. 내 코드에 framelayout이 있지만 둘 다 똑같아 보인다. – ruben

관련 문제