문제

I have an ImageView that I set to center inside the LinearLayout of a basic page in my xml file. I snap a photo, detect for faces and display it on my ImageView. The problem is, it always displays on the left side of my phone (I have the app permanently set to portrait mode)...I want the width to be fill_parent and I want the height to be fixed, but any change I make does nothing to how it is displayed.

Here is my xml code:

<ImageButton 
     android:id="@+id/YesButtonTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/yesface"  
     android:layout_gravity="top|center" />

<ImageView
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:id="@+id/image_view"
     android:gravity="center"
     android:layout_weight="1.0"/>

<TextView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:id="@+id/message"
     android:gravity="center_horizontal"
     android:padding="10dip"/>

 <Button
     android:layout_gravity="center_horizontal"
     android:layout_width="fill_parent"
     android:textStyle="bold" 
     android:id="@+id/action_button" 
     android:selectAllOnFocus="false" 
     android:layout_height="100dip" 
     android:text="Click here to crop detected face"/>

Here is where I set the bitmap to the ImageView:

((Button) findViewById(R.id.action_button))
        .setOnClickListener(btnClick);
 mTheMessage.setText(R.string.faceMessage);
mThePicture = (ImageView) findViewById(R.id.image_view);
mThePicture.setImageBitmap(bitmap565);

bitmap565 is defined as:

Bitmap bitmap565 = Bitmap.createBitmap(width, height, Config.RGB_565);

then compressed:

try {
    FileOutputStream fos = new FileOutputStream(filepath);
    bitmap565.compress(CompressFormat.JPEG, 90, fos);
    fos.flush();
    fos.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Before I display the bmp on the ImageView. Anyone have any ideas why it will not behave and will the ImageView correctly?

도움이 되었습니까?

해결책

This worked for me, maybe it will work for you as well.

 <ImageView
    android:id="@+id/image_id"
    android:padding="2dip"
    android:layout_width="fill_parent" 
     android:layout_height="wrap_content"
     android:scaleType="centerInside"
     android:layout_weight="1"/>

As to why it your xml doesn't work. I looked and everything seems fine except maybe for the android:gravity that seems to do nothing in the case of images.

다른 팁

Set layout_width="wrap_content" not fill_parent, Then only you will get effect of the parent Layout gravity

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/image_view"
        android:gravity="center"
        android:layout_weight="1.0"/>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top