如何设置RelativeLayout的高度等于它的宽度? [英] How to set relativelayout height equal to its width?
问题描述
我搜索了很多,找出其要去工作,我怎么什么也没得到。
我发现一些Java code动态地做到这一点,但没有工作的主题蚂蚁和打开程序时,显示一个强制关闭。
这是我的code:
I searched a lot to find out how its gonna work and I got nothing. I found some java code to do it dynamically but didn't work ant of theme and when opening the program, show a force close. this is my code:
<LinearLayout android:baselineAligned="false"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/rlv1"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_weight="1"
android:background="@drawable/rectengle2" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/rlv2"
android:layout_width="fill_parent"
android:layout_weight="1"
android:background="@drawable/rectengle" >
</RelativeLayout>
</LinearLayout>
你可以看到我创建一个矩形,并用lineaer布局父把它用在一个相对的布局和我分裂线性布局verticali两个是对每一个相对布局。
现在我想将这些相对布局的高度等于其宽度得到一个纯正的方矩形。
我尝试过很多方法,但......能不能帮我任何人请? thnaks ..
as you can see I created a rectangle shape and used it in a relative layout with a lineaer layout as parent and i splitted that linear layout verticali to two part for each relative layout. now i want to set these relative layout height equal to its width to get a pure square rectangle. i tried many way but ... can help me any guy please?? thnaks..
推荐答案
最后我自己发现了什么在这种情况下做的。所以我要解释如何解决为那些谁拥有或将有我的问题。
在我的XML文件中的用户界面我设置layout_weight = 1,一对相对的布局后,其中的线性布局在屏幕上进行分裂,实际上他们每个人的宽度是我的屏幕宽度的一半。我的问题是关于身高和我试过很多的java codeS设置相对布局的高度等于它的宽度。
最后我计算了DP期限的屏幕宽度和每一个亲人通信布局的高度设置为这一点。这是解决code:
Finally I myself found out what to do in this situation. so i'm gonna explain how to solve for those who have or will have my problem. in my xml ui file I set layout_weight=1 for a pair of relative layout that were in a linear layout to be splitted in the screen and actually width of each of them be the half of my screen width. my problem was about height and i tried many java codes to set relative layout height equals to its width. finally i calculated the screen width in "DP" Term and set every relative layout's height to this. this is the solved code:
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
float dpWidth = outMetrics.widthPixels / density;
RelativeLayout Rl1 = (RelativeLayout) findViewById(R.id.rlv1);
Rl1.getLayoutParams().height = (int) (dpWidth);
RelativeLayout Rl2 = (RelativeLayout) findViewById(R.id.rlv2);
Rl2.getLayoutParams().height = (int) (dpWidth);
所以我得到了一个纯正的方!好好享受。谢谢
so i got a pure square! Enjoy it. Thanks
这篇关于如何设置RelativeLayout的高度等于它的宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!