Avoiding a big image push other layouts out of the screen
I've tried for hours but nothing achieved.
Thats my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<LinearLayout
android:layout_height="wrap_content"
android:id="@+id/linearLayout1"
android:orientation=&开发者_StackOverflow社区quot;vertical"
android:layout_width="wrap_content"
android:layout_gravity="center">
<TextView
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="A dog"
android:id="@+id/textView1"
android:textSize="50sp"></TextView>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/dog"
android:id="@+id/imageView1"></ImageView>
</LinearLayout>
</LinearLayout>
As you can see in the following screen everything is ok in small and big screens
But what I really need is to put the text below the image. Changing their order in the container the result is:
It works nice for screens taller than the image but for small screens the text is not visible. How can I solve that issue? Please for the solution consider those points:
- The layout that contains the image and text must be centered in his parent (for that example the base layout) (especially for long screens)
- The drawable should keep its original size if screen is big enought, and reduced (keeping the aspect ratio) when the screen is smaller
Thanks in advance
You could scale the image to fit a certain width and height
Refer here Scale
EDIT:
You could try something like
public static final ImageView.ScaleType CENTER_INSIDE
Since: API Level
Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding). The image is then centered in the view. From XML, use this syntax:
android:scaleType="centerInside
EDIT: Also you may be able to use Bitmap..
Something like..
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int newWidth = 640;
int newHeight = 480;
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// create the new Bitmap object
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,
height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
ImageView imageView = new ImageView(this);
imageView.setImageDrawable(bmd);
imageView.setScaleType(ScaleType.CENTER);
linearLayout.addView(imageView, new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
setContentView(linearLayout); }
}
I tested that code and it have the behaviour you are looking for. Let me know if you need more help
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1"
>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_gravity="center"
>
<LinearLayout
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_width="wrap_content"
android:id="@+id/linear2"
android:layout_alignParentBottom="true"
></LinearLayout>
<TextView
android:layout_above="@id/linear2"
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
></TextView>
<ImageView
android:id="@+id/imageiffel"
android:src="@drawable/my_image"
android:layout_above="@id/textView1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
></ImageView>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
精彩评论