2014-06-13 3 views
0

선형 레이아웃 내부에 이미지가 있습니다. 이미지의 크기가 작아서 전체 화면을 덮지 않습니다. 이미지를 화면 중앙에 배치하고 싶습니다. 나는 다음과 같은 코드를 사용 :Android 스타일링

<LinearLayout 
    android:id="@+id/ll4" 
    android:layout_height="50dp" 
    android:layout_width="match_parent" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/exit" 
     android:layout_width="match_parent"   
     android:layout_height="wrap_content" 
     android:onClick="exitApplication" 
     android:contentDescription="exit"   
     android:layout_gravity="center_horizontal" 
     android:gravity="center" 
     android:src="@drawable/exitbutton" > 
    </ImageView> 
</LinearLayout> 

을하지만이 직면하고있는 문제는 이미지의 측면에 수평으로 아무 곳이나 클릭하면 onClick 함수가 호출되는 것입니다. 이 문제를 어떻게 해결할 수 있습니까?

+0

작동은 당신의 이미지가 화면의 전체 폭에 걸쳐 그래서 물론 onClick이 호출 될 것입니다. – tyczj

+0

대신 이미지를 'RelativeLayout'에 배치하십시오. d는 너비와 높이를 모두 'wrap_content'로 만든다. 그런 다음 레이아웃을 이미지 가운데에 놓을 수 있습니다. – 323go

+0

tyczj, 나는 또한 그 버그가 오는 이유를 알고 – user3726986

답변

2

만들기 wrapcontentimageView의 폭과 gravity

linearLayout을 시도해 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/ll4" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:orientation="horizontal" 
    android:gravity="center_horizontal" > 

    <ImageView 
     android:id="@+id/exit" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:contentDescription="exit" 
     android:onClick="exitApplication" 
     android:src="@drawable/dolphin1" /> 

</LinearLayout> 

희망이

+0

thanks manishika !! 효과가있다. – user3726986

0

주석에서 말했듯이 이미지는 화면의 전체 너비를 차지합니다.

android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:layout_centerInParent="true" 

를 메인 레이아웃으로 RelativeLayout를 사용 :이 문제를 해결하려면 당신은 당신의 ImageView이를 사용해야합니다.

+0

이것은 이미지가 중앙에 오지 않아서이 문제를 해결하지 못했습니다 – user3726986

0

이 당신을 위해 무엇을 찾고있는 사람입니다 :

LinearLayout :

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

레이아웃은 (정기적으로) 화면을 작성해야합니다. 그렇기 때문에 layout_heightlayout_width 속성을 match_parent으로 설정 한 것입니다.

ImageView :

<ImageView> 
    ... 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
</ImageView> 

ImageView 그렇지 않으면 match_parent와 그 부모의 폭을 채울 것 특정 폭의 크기를해야하거나 wrap_content로 설정 될 수있다. 레이아웃과 이미지 사이에 여유 공간이 있기 때문에 layout_gravity 속성은 화면의 크기와 동일한 레이아웃 내부에 ImageView을 중심에 배치합니다. 데이터를 다음

0
Try this way,hope this will help you to solve your problem. 

<LinearLayout 
     android:id="@+id/ll4" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:gravity="center"> 

     <ImageView 
      android:id="@+id/exit" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="exitApplication" 
      android:contentDescription="exit" 
      android:adjustViewBounds="true" 
      android:src="@drawable/exitbutton" > 
     </ImageView> 
    </LinearLayout>