我已经设置了一个相对视图,里面有我所有的元素(按钮,图像等)。它是我的Android应用程序的扉页。
现在我想将“Lite”横幅覆盖在整个布局上,在左上角。
我的问题是“Lite”横幅图像是一个倾斜的红色橡胶,我需要在屏幕上将它的顶部点设置为(-45,-45),以便只显示我想要的图像的一部分(附上源图像,以便您可以了解图像的哪些部分应该在屏幕上可见)。
我尝试了AbsoluteLayout和RelativeLayout,用SetLeft和SetTop以编程方式移动它,但负值不被接受。
知道吗?
您可以使用属性android:cliptopadding=“false”
的相对布局来获得所需的效果。
示例:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clipToPadding="false"
android:background="@android:color/white"
android:paddingLeft="50dip"
>
<ImageView
android:id="@+id/myId"
android:layout_width="60dip"
android:layout_height="60dip"
android:layout_marginLeft="-70dp"
android:layout_marginTop="-20dp"
android:clipChildren="false"
android:src="@drawable/button_normal" />
</RelativeLayout>
结果:
我想和大家分享我在这件事上的经验。
我的想法是在我的应用程序主屏幕的顶角显示一个倾斜的“精简”橡胶。
罗德·阿尔冈昆的回答很好。但是,它并没有完全解决我的问题,因为我必须使图片的尺寸适应屏幕高度。。。和屏幕方位。恶梦。即使使用相对布局,这也几乎是不可能的,因为图像的隐藏部分从来没有正确地对齐过。
所以我必须改变工作方式:图片必须左移和上移20%。怎么做?
1)在layout.xml文件中:
>
配置ImageView,使其适合容器相关Layout的宽度和高度(layout_width=“wrap_content”和layout_height=“wrap_content”)
<RelativeLayout
android:id="@+id/accueil_litebannerlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/accueil_litebanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/lite_banner" />
</RelativeLayout>
2)在Activity.java类文件中:
//get screen dimensions
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int ScreenWidth = size.x;
int ScreenHeight = size.y;
//set the desired height of the rubber, based on screen's height
int myLayoutWidthAndHeight=ScreenHeight/4;
//get rubber PNG image dimensions
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeResource(getResources(),
R.drawable.lite_banner,options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
//redux_factor has to be calculated, because if the image is reduced, then the translation has to be adapted
double redux_factor=1;
if (myLayoutWidthAndHeight<imageWidth) {
redux_factor=(double)myLayoutWidthAndHeight/imageWidth;
}
//determine by how many pixels left and top (same) the image will have to be translated
double translation_percents=.22;
double myCroppedMargin_double=imageWidth*translation_percents*redux_factor;
int myCroppedMargin=(int) Math.round(myCroppedMargin_double);
//get the image layout
RelativeLayout litebannerlayout=(RelativeLayout) this.findViewById(R.id.accueil_litebannerlayout);
//change its parameters (width, height, leftMargin, topMargin)
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(myLayoutWidthAndHeight,myLayoutWidthAndHeight);
params.setMargins(-myCroppedMargin, -myCroppedMargin, 0,0);
litebannerlayout.setLayoutParams(params);
嗯哼。很管用。。。
您可以使用此示例代码将imageView移出屏幕,或者基于百分比,或者基于像素计数。此代码也可以调整为将橡胶/横幅放在右上角,左下角,右下角。
好吧,我们继续谈别的吧。。。