如何设置适合任何屏幕高度的布局?
问题描述:
我有一些关于我的应用程序的布局问题。 这是我的应用程序布局:如何设置适合任何屏幕高度的布局?
<?xml version="1.0" encoding="utf-8"?>
<ImageView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:src="@drawable/tax_calculator_logo"/>
<LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent"
android:orientation="vertical" android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" android:layout_gravity="center"
android:layout_marginTop="10dp" android:layout_marginBottom="10dp">
<Button android:id="@+id/contactUs" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="Contact Us"
android:textSize="20dp"/>
<Button android:id="@+id/contactUs" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="Contact Us"
android:textSize="20dp"/>
<Button android:id="@+id/contactUs" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="Contact Us"
android:textSize="20dp"/>
<Button android:id="@+id/contactUs" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="Contact Us"
android:textSize="20dp"/>
</LinearLayout>
在这里,我要定这一切按钮是完全装配与任何设备的屏幕的高度。 是的,我可以显示所有的按钮。但我希望它们之间有相等的空间,它们必须适合屏幕高度。 那我该怎么做呢?
编辑:
且有,我tax_calculator_logo是600x239分辨率的,我已经设置ImageView的高度WRAP_CONTENT和宽度与FILL_PARENT那么为什么图像从底部和顶部croping ???
答
为每个按钮赋予相同的权重,他们将自动共享高度。
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:orientation="vertical" >
<Button
android:id="@+id/contactUs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Contact Us"
android:textSize="20dp" />
<Button
android:id="@+id/contactUs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Contact Us"
android:textSize="20dp" />
<Button
android:id="@+id/contactUs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Contact Us"
android:textSize="20dp" />
<Button
android:id="@+id/contactUs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Contact Us"
android:textSize="20dp" />
</LinearLayout>
答
要做到这一点,我们必须让他显示(屏幕)宽度和高度。然后通过java代码分配高度。对于exmple
XML文件是这样
<ImageView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:src="@drawable/tax_calculator_logo"/>
<LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent"
android:orientation="vertical" android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" android:layout_gravity="center"
android:layout_marginTop="10dp" android:layout_marginBottom="10dp">
<Button android:id="@+id/contactUs" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="Contact Us"
android:textSize="20dp"/>
<Button android:id="@+id/contactUs" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="Contact Us"
android:textSize="20dp"/>
<Button android:id="@+id/contactUs" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="Contact Us"
android:textSize="20dp"/>
<Button android:id="@+id/contactUs" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="Contact Us"
android:textSize="20dp"/>
在活动类
Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight()/5;
的setContentView后(..);
imageview.getLayoutParams().height=height;
button1.getLayoutParams().height=height;
button2.getLayoutParams().height=height;
button3.getLayoutParams().height=height;
button4.getLayoutParams().height=height;
我觉得这可能会帮助你...
哇。它的工作很棒。谢谢。你能给我一些关于体重的想法吗?我的意思是我如何使用它?它会做什么? –
精湛.....解决方案。 –
请参阅我编辑的问题。 。 。 –