2016-06-29 9 views
1

現在、画面の高さの半分しかかからないレイアウトでは、私は4 ImageButtonを持っています。 ImageButtonをレイアウトに均等に配置したいと思います。ここで私が持っているものです。レイアウト内のImageButtonを均等に整列する方法

<?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> 

これは動作しますが、ここでの結果であることから:

enter image description here

問題は、1:私は、各ボタンの下にテキストを追加したいと私はないですどのように動作するのか、2:ボタンの一部であるかのように、ImageButtonの左右の白い点がアクティブになっています。これを行うより良い方法はありますか?

+0

FrameLayout内にTextViewと共にImageButtonを配置することができます。 – user5195185

+0

ボタンであるため、「アクティブです」。特定のサイズの背景画像を設定し、ImageButtons(これは「インテリジェント」に拡張します)にlayout_weight = "1"で "0dp"を設定しました。ボタンやラッパーに "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

とQUEの第二部のためのような何かを行うことができます相対レイアウト全体でクリックリスナを設定する必要があります。 JAVA

XML

android:id="@+id/section1" 

findviewbyid(R.id.section1).setOnClickListener(...)

+0

ありがとう!ありがとう! –

関連する問題