用3个视图水平填充整个屏幕?
我想用3个视图水平填充整个屏幕。我希望他们都是相同的大小。如果我用dp手动输入宽度的值,其中一个比另一个要大,或者在更换设备时它不会填满大的屏幕。什么是正确的做法呢? 用3个视图水平填充整个屏幕?
您可以使用具有“fill_parent”宽度属性的父布局,然后为每个视图给出layout_weight =“1”。
试试这个。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<Button android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Text"
android:layout_weight="1"/>
<Button android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Text"
android:layout_weight="1"/>
<Button android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Text"
android:layout_weight="1"/>
</LinearLayout>
我想评论,但我没有足够的代表。所以我发布这个答案。你是用水平的LinearLayout
来包装这三个?如果是,您是否尝试过使用属性android:layout_weight
?如果还没有,请在布局XML中为所有这些视图设置android:layout_weight="1"
。这应该平等地看待这些观点。但是,如果您可以发布您的XML代码,那将会更有帮助。
现在你可以发表评论.. :) :)你给权利ans但适当的必要的解释哪个人发布问题,他们不知道权重,所以如果你写这个,然后他们不会理解。因为如果他们知道体重,那么他不会在这里发表问题..对。错过:) –
@VishalHalani谢谢。 :)下次我会记住这一点。 –
试试这个代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Test" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Test" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Test" />
</LinearLayout>
</RelativeLayout>
你可以像这样,我只是将按钮你写其他componants也
<LinearLayout xmlns:android="http://schemas.android.com/apk/re/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Test" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Test" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Test" />
</LinearLayout>
什么,因为 – raj
ü试图建立一个相对布局,并在此创建线性布局与水平横向也给予重量1线性布局 – raj
每个按钮,并且不要忘记给父线性布局权重总和= 3。 – Praneeth