Android 将宽度调整为半屏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11244326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adjust width to half screen
提问by urSus
I came across this link :
我遇到了这个链接:
Assign width to half available screen width declaratively
How to do the same in relative layout? Here's the code: Trying linearlayout inside relative layout. But this doesn't work.
如何在相对布局中做同样的事情?这是代码:在相对布局中尝试线性布局。但这不起作用。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/button5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:text="UID"
android:textColor="#ffe5f1f1" />
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffe5f1f1"
android:hint="Enter UID"
android:inputType="number"
android:shadowColor="5"
android:textColor="#ff009999"
android:textStyle="bold"
android:typeface="monospace" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentLeft="true"
android:layout_marginTop="18dp"
android:text="Button" />
<Button
android:id="@+id/button8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button7"
android:layout_weight="1"
android:layout_marginTop="16dp"
android:text="Button" />
<Button
android:id="@+id/button9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button8"
android:layout_marginTop="17dp"
android:text="Button" />
<EditText
android:id="@+id/editText2"
android:layout_width="50"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_weight="1"
android:background="#ffe5f1f1"
android:ems="10"
android:hint="Enter Name"
android:inputType="number"
android:inputType="textPersonName"
android:textColor="#ff009999"
android:textStyle="bold"
android:layout_weight="1"
android:typeface="monospace" />
<EditText
android:id="@+id/editText3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:ems="10"
android:layout_weight="1"
android:inputType="number" />
<EditText
android:id="@+id/editText4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button7"
android:layout_alignBottom="@+id/button7"
android:layout_alignParentRight="true"
android:ems="10"
android:layout_weight="1"
android:inputType="phone" />
<EditText
android:id="@+id/editText5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button8"
android:layout_alignBottom="@+id/button8"
android:layout_alignParentRight="true"
android:ems="10"
android:layout_weight="1"
android:inputType="textPersonName" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button9"
android:layout_alignBottom="@+id/button9"
android:layout_alignLeft="@+id/editText5"
android:layout_weight="1"
android:text="Male" />
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/radioButton2"
android:layout_alignBottom="@+id/radioButton2"
android:layout_alignParentRight="true"
android:layout_weight="1"
android:text="Female" />
<Button
android:id="@+id/button10"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button9"
android:layout_marginTop="31dp"
android:layout_weight="1"
android:text="Button" />
<Spinner
android:id="@+id/spinDept"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button10"
android:layout_alignBottom="@+id/button10"
android:layout_alignLeft="@+id/radioButton2"
android:layout_alignParentRight="true"
android:layout_weight="1"
android:background="#ff009999" />
</LinearLayout>
</RelativeLayout>
回答by urSus
This is a nice workaround if you precisely need half, when using RelativeLayout. What you do is, make a fake invisible view centered in parent, and have left button be alignParentLeft and alignRight to that fake view and the right button vice-versa
如果您在使用 RelativeLayout 时正好需要一半,这是一个很好的解决方法。你要做的是,制作一个以父级为中心的假不可见视图,左按钮为 alignParentLeft 和 alignRight 到该假视图,右按钮反之亦然
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<View android:id="@+id/fakeView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/fakeView"
android:layout_alignParentLeft="true"
android:text="Left"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/fakeView"
android:layout_alignParentRight="true"
android:text="Right"/>
</RelativeLayout>
Its really clever.
它真的很聪明。
回答by Rinkal Bhanderi
Wrap the same thing with Relative layout which should work for you.
用相对布局包装同样的东西,它应该适合你。
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="somebutton">
<TextView android:layout_width="fill_parent"
android:layout_height="Wrap_content"
android:layout_weight="1">
</LinearLayout>
回答by Dmide
回答by Swayam
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
plannerEditLayout = (RelativeLayout) findViewById(R.id.plannerLayoutEdit);
plannerEditLayout.setGravity(Gravity.CENTER_VERTICAL);
plannerEditLayout.setLayoutParams(new RelativeLayout.LayoutParams(
(width / 2), height));
I use the same code for LinearLayout. Havent tried it out myslef for RelativeLayout. Give it a shot. Comment if it doesn't work.
我对 LinearLayout 使用相同的代码。还没有尝试过 myslef 的相对布局。试一试。如果它不起作用,请发表评论。
回答by MobileEvangelist
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:layout_width="match_parent"
android:id="@+id/button"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="somebutton">
<TextView android:layout_width="match_parent"
android:id="@+id/textview"
android:layout_height="Wrap_content"
android:layout_weight="1">
</LinearLayout>
In above example used LinearLayout, set widget layout_width to "match_parent" and provide weight(to specify area to be occupy).
在上面使用 LinearLayout 的示例中,将小部件 layout_width 设置为“match_parent”并提供权重(指定要占用的区域)。
This solves your problem.
这可以解决您的问题。