【发布时间】:2014-12-19 03:43:32
【问题描述】:
所以我在这里阅读了很多问题,并尝试了很多东西来尝试让它发挥作用。我要么误解了我读过的所有示例,要么正在查看错误的内容。
我要做的是在包含图像视图的相对布局中设置相对布局。我希望能够使相对布局与图像视图的大小相匹配(我希望根据特定的屏幕大小进行缩放)以保持恒定的正方形大小。这个视图也将包含按钮。当我运行下面的代码时,我得到一个正方形的图像视图,但按钮显示在屏幕顶部。所以在我看来,图像视图正确地缩小为正方形,但布局本身仍然与整体屏幕尺寸相匹配。我有一个 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 是一个正方形,但我希望它根据屏幕大小改变大小
标签: android view imageview scale android-relativelayout