0
所以我已经阅读了很多关于这里的问题以及尝试了很多东西来试图让这个工作。我要么误解我读过的所有例子,要么正在看错误的东西。平铺一个相对的布局/图像视图
我想要做的是在包含图像视图的相对布局内设置相对布局。我希望能够使相对布局匹配imageview的大小(我希望根据特定的屏幕大小缩放)以保持常量的方形大小。这个视图也包含按钮。当我运行下面的代码时,我得到了一个用于图像视图的正方形,但按钮显示在屏幕的顶部。所以在我看来,imageview正确地缩小到一个正方形,但布局本身仍然匹配整个屏幕大小。我已成立了一个XML文件,如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="@+id/layout_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
>
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="@drawable/bg" />
<Button
android:id="@+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_one" />
....
</RelativeLayout>
</RelativeLayout>
的Java代码如下:
public class MainActivity extends ActionBarActivity {
RelativeLayout layoutTwo, mainLayout;
Button buttonOne, buttonTwo, buttonThree, buttonFour, buttonFive;
Button buttonSix, buttonSeven, buttonEight, buttonNine;
ImageView scalingBackground;
int screenWidth, screenHeight, finalSquare;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainLayout = (RelativeLayout) findViewById(R.id.main_layout);
layoutTwo = (RelativeLayout) findViewById(R.id.layout_two);
scalingBackground = (ImageView) findViewById(R.id.image_view);
initializeButtons();
squareBackground();
}
private void squareBackground() {
screenWidth = mainLayout.getLayoutParams().width;
scalingBackground.getLayoutParams().height = screenWidth;
scalingBackground.getLayoutParams().width = screenWidth;
layoutTwo.getLayoutParams().height = screenWidth;
layoutTwo.getLayoutParams().width = screenWidth;
}
不太清楚什么怎么回事,我会在正确的方向欣赏踢。谢谢
图像bg是否总是正方形? – 2014-12-19 03:51:45
是的,我总是希望bg是一个正方形,但我希望它根据屏幕大小更改大小 – user3803709 2014-12-19 13:11:38