簡體   English   中英

調整寬度為半屏

[英]Adjust width to half screen

我遇到了這個鏈接:

以聲明方式將寬度分配給可用屏幕寬度的一半

如何在相對布局中做同樣的事情? 這是代碼:在相對布局中嘗試線性布局。 但這不起作用。

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

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

它真的很聰明。

使用適合您的相對布局包裝相同的東西。

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

除了urSus的答案,最好使用Space類而不是View

   <Space android:id="@+id/fakeView"
       android:layout_width="0dp"
       android:layout_height="0dp" 
       android:layout_centerInParent="true"/>
    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 使用相同的代碼。 還沒有嘗試過 myslef 的 RelativeLayout。 試一試。 如果它不起作用,請發表評論。

像這樣嘗試將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> 
<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”並提供權重(以指定要占用的區域)。

這解決了你的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM