2016-06-29 6 views
1

현재 화면 높이의 절반 만 차지하는 레이아웃에 4 ImageButton이 있습니다. ImageButton을 레이아웃에 고르게 배치하고 싶습니다.레이아웃에서 ImageButton을 균등하게 정렬하는 방법

enter image description here

문제는, 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="match_parent" 
android:orientation="vertical"> 

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

    <ImageButton 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:src="@mipmap/feed_button" 
     android:background="@android:color/transparent" 
     android:layout_weight="1" /> 

    <ImageButton 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:src="@mipmap/feed_button" 
     android:background="@android:color/transparent" 
     android:layout_weight="1" /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="40dp" 
    android:orientation="horizontal"> 

    <ImageButton 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:src="@mipmap/feed_button" 
     android:background="@android:color/transparent" 
     android:layout_weight="1" /> 

    <ImageButton 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:src="@mipmap/feed_button" 
     android:background="@android:color/transparent" 
     android:layout_weight="1" /> 

</LinearLayout> 

</LinearLayout> 

는 결과이다 : 나는 각 버튼 아래에 텍스트를 추가하고 싶습니다 내가 아니라고 여기에 내가 무엇을 가지고 이것이 어떻게 작동 할 것이며, 2 : 버튼의 일부인 것처럼, 의 오른쪽과 왼쪽에있는 흰 반점이 활성화되어 있습니다. 이 작업을 수행하는 더 좋은 방법이 있습니까?

+0

FrameLayout 내에 TextView와 함께 ImageButton을 넣을 수 있습니다. – user5195185

+0

버튼이기 때문에 "This are active"입니다. 특정 크기의 배경 이미지와 layout_weight = "1"로 "0dp"를 ImageButtons에 설정했습니다 (이는 "지능적으로"확장됩니다). 버튼이나 래퍼에 "wrap_content"를 넣고 여백을 사용하여 작업 할 수 있습니다. – user5195185

답변

1

다른 -를 첫째 셀에 대한 예를 들면 수돗물 (그리고 더 나은) 대안은 TableLayout을 사용하는 것입니다 :

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

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

     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@android:color/transparent"> 

      <ImageButton 
       android:id="@+id/btn1" 
       android:contentDescription="@null" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerInParent="true" 
       android:src="@mipmap/feed_button" 
       android:background="@android:color/transparent"/> 

      <TextView 
       android:id="@+id/txt1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/btn1" 
       android:layout_marginTop="10dp" 
       android:layout_centerHorizontal="true" 
       android:text="Text 1"/> 

     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@android:color/transparent"> 

      <ImageButton 
       android:id="@+id/btn2" 
       android:contentDescription="@null" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerInParent="true" 
       android:src="@mipmap/feed_button" 
       android:background="@android:color/transparent"/> 

      <TextView 
       android:id="@+id/txt2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/btn2" 
       android:layout_marginTop="10dp" 
       android:layout_centerHorizontal="true" 
       android:text="Text 2"/> 

     </RelativeLayout> 
    </TableRow> 

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

     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@android:color/transparent"> 

      <ImageButton 
       android:id="@+id/btn3" 
       android:contentDescription="@null" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerInParent="true" 
       android:src="@mipmap/feed_button" 
       android:background="@android:color/transparent"/> 

      <TextView 
       android:id="@+id/txt3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/btn3" 
       android:layout_marginTop="10dp" 
       android:layout_centerHorizontal="true" 
       android:text="Text 3"/> 

     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="@android:color/transparent"> 

      <ImageButton 
       android:id="@+id/btn4" 
       android:contentDescription="@null" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerInParent="true" 
       android:src="@mipmap/feed_button" 
       android:background="@android:color/transparent"/> 

      <TextView 
       android:id="@+id/txt4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/btn4" 
       android:layout_marginTop="10dp" 
       android:layout_centerHorizontal="true" 
       android:text="Text 4"/> 

     </RelativeLayout> 

    </TableRow> 

</TableLayout> 
+0

와우! 다른 대답은 좋았지 만, 당신의 대답은 훌륭합니다! 나는 당신의 코드에 대한 경고 메시지가 전혀없는 것을 좋아한다. 대단히 감사합니다 !! –

+0

그것은 나의 기쁨입니다. (; – user5195185

2

같은

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_gravity="center" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:orientation="horizontal"> 

    <RelativeLayout 
     android:id="@+id/section1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@android:color/transparent"> 

     <ImageButton 
      android:id="@+id/img1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" 
      android:src="@mipmap/ic_launcher" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/img1" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="10dp" 
      android:text="info 1" 
      android:textColor="#fff" 
      android:textSize="25sp" /> 
    </RelativeLayout> 


    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@android:color/transparent"> 

     <ImageButton 
      android:id="@+id/img2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" 
      android:src="@mipmap/ic_launcher" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/img2" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="10dp" 
      android:text="info 2" 
      android:textColor="#fff" 
      android:textSize="25sp" /> 
    </RelativeLayout> 


</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_gravity="center" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:orientation="horizontal"> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@android:color/transparent"> 

     <ImageButton 
      android:id="@+id/img3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" 
      android:src="@mipmap/ic_launcher" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/img3" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="10dp" 
      android:text="info 3" 
      android:textColor="#fff" 
      android:textSize="25sp" /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@android:color/transparent"> 

     <ImageButton 
      android:id="@+id/img4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_weight="1" 
      android:background="@android:color/transparent" 
      android:src="@mipmap/ic_launcher" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/img4" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="10dp" 
      android:text="info 4" 
      android:textColor="#fff" 
      android:textSize="25sp" /> 
    </RelativeLayout> 

</LinearLayout> 

결과

enter image description here

와 가야의 두 번째 부분에 대한 작업을 수행 할 수 있습니다 당신은 전체 상대적 레이아웃에서 클릭 리스너를 설정해야합니다.

XML

android:id="@+id/section1" 

JAVA

findViewById를 (R.id.section1) .setOnClickListener (...)

+0

고마워요! 고마워요! –