调整宽度为屏幕的一半

8
我看到了这个链接:

声明式地将宽度分配给半个可用屏幕宽度

如何在RelativeLayout中进行相同的操作? 下面是代码: 尝试在RelativeLayout中使用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>

你真正想要深入研究屏幕布局还是它的小部件? - MobileEvangelist
制作一个表单布局,该布局适用于所有屏幕尺寸。 - user927026
我不明白你的问题。链接的问题是关于将视图分成两半的。你的线性布局中有十几个视图。你想做什么? - njzk2
6个回答

57

如果您在使用RelativeLayout时需要精确一半的位置,那么这是一个不错的解决方法。你要做的就是,在父视图中心创建一个假的不可见视图,并将左侧按钮与该假视图对齐并右对齐,右侧按钮反之。

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

它真的很聪明。


我同意,非常聪明。我很想知道这是否比使用LinearLayout更快渲染。 - zarazan
不知道,可能不是,但从更一般的角度来看,如果你一开始就使用相对布局,这会更快,因为你可以消除嵌套布局。 - urSus

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>

那我需要多次使用线性布局吗?我在相对布局中有近5行类似的情况。 - user927026
在一个相对布局内,您可以有一个线性布局,其中包含您的所有行,无论是一个、两个还是 100 个。 - RPB

4
除了urSus的回答外,最好使用Space类来代替View
   <Space android:id="@+id/fakeView"
       android:layout_width="0dp"
       android:layout_height="0dp" 
       android:layout_centerInParent="true"/>

1
我不同意。Space仅适用于api14+,如果您想针对广泛的设备进行定位,则几乎没有用处。 - njzk2
好的,API14+是4.0+,根据最新的平台分布图(http://goo.gl/fmFDJw),API<14的设备数量现在约为15%,已经几乎可以忽略不计了。 - Dmide
2
如果你认为15%微不足道,那很好。此图仅测量可以访问 Google Play 商店的设备,这远远不是所有设备。 - njzk2

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。试一下吧。如果不起作用,请留言评论。


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> 

-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,将小部件的layout_width设置为“match_parent”,并提供权重(以指定要占用的区域)。
这解决了您的问题。

不对。每个元素都有定义的id。这并不重要。 - user927026

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接