将宽度调整为半屏

时间:2012-06-28 12:10:00

标签: android relativelayout

我遇到了这个链接:

Assign width to half available screen width declaratively

如何在相对布局中做同样的事情? 这是代码:在相对布局中尝试linearlayout。但这不起作用。

<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>

6 个答案:

答案 0 :(得分:53)

如果您在使用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>

非常聪明。

答案 1 :(得分:4)

使用相对布局包裹同样的东西,它应该适合你。

<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>

答案 2 :(得分:2)

urSus 的答案外,最好使用Space课程代替View

   <Space android:id="@+id/fakeView"
       android:layout_width="0dp"
       android:layout_height="0dp" 
       android:layout_centerInParent="true"/>

答案 3 :(得分:1)

    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));

我对LinearLayout使用相同的代码。没有为RelativeLayout自己尝试过。试一试。评论是否不起作用。

答案 4 :(得分:0)

尝试 layout_weight 到 1 和 layout_width 到 fill_parent 像这样,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_layout"
    android:orientation="vertical"
    android:padding="10dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
           
        </LinearLayout>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            
        </LinearLayout>
    </LinearLayout>
</LinearLayout> 

答案 5 :(得分:-1)

<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>

在上面使用的LinearLayout示例中,将widget layout_width设置为“match_parent”并提供权重(指定要占用的区域)。

这解决了您的问题。

相关问题