调整宽度半屏 [英] Adjust width to half screen

查看:139
本文介绍了调整宽度半屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到这个链接来了

<一个href=\"http://stackoverflow.com/questions/2581481/assign-width-to-half-available-screen-width-declaratively\">Assign宽度的一半可用屏幕宽度声明

如何做到在相对布局一样吗?
这里的code:尝试的LinearLayout相对布局内。但是,这是行不通的。

 &LT;的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:背景=@绘制/ BG&GT;    &LT;的LinearLayout
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:方向=横向&GT;        &LT;按钮
            机器人:ID =@ + ID / button5
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_marginTop =14dp
            机器人:layout_weight =1
            机器人:文字=按钮/&GT;        &LT;按钮
            机器人:ID =@ + ID / button6
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_marginTop =16DP
            机器人:layout_weight =1
            机器人:文字=按钮/&GT;        &LT;按钮
            机器人:ID =@ + ID /将Button4
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentLeft =真
            机器人:layout_alignParentTop =真
            机器人:layout_weight =1
            机器人:文字=UID
            机器人:文字颜色=#ffe5f1f1/&GT;        &LT;的EditText
            机器人:ID =@ + ID / editText1
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1
            机器人:背景=#ffe5f1f1
            机器人:提示=输入UID
            安卓的inputType =数字
            安卓则shadowColor =5
            机器人:文字颜色=#ff009999
            机器人:文字样式=大胆
            机器人:字体=等宽&GT;            &LT; requestFocus的/&GT;
        &LT; /&的EditText GT;    &LT;按钮
        机器人:ID =@ + ID / button7
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1
        机器人:layout_alignParentLeft =真
        机器人:layout_marginTop =18dp
        机器人:文字=按钮/&GT;    &LT;按钮
        机器人:ID =@ + ID /按钮8
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ + ID / button7
            机器人:layout_weight =1
        机器人:layout_marginTop =16DP
        机器人:文字=按钮/&GT;    &LT;按钮
        机器人:ID =@ + ID /按钮9
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ + ID /按钮8
        机器人:layout_marginTop =17dp
        机器人:文字=按钮/&GT;    &LT;的EditText
        机器人:ID =@ + ID / editText2
        机器人:layout_width =50
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:layout_weight =1
        机器人:背景=#ffe5f1f1
        机器人:EMS =10
        机器人:提示=输入名称
        安卓的inputType =数字
        安卓的inputType =textPersonName
        机器人:文字颜色=#ff009999
        机器人:文字样式=大胆
            机器人:layout_weight =1
        机器人:字体=等宽/&GT;    &LT;的EditText
        机器人:ID =@ + ID / editText3
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:EMS =10
            机器人:layout_weight =1
        安卓的inputType =号/&GT;    &LT;的EditText
        机器人:ID =@ + ID / editText4
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID / button7
        机器人:layout_alignBottom =@ + ID / button7
        机器人:layout_alignParentRight =真
        机器人:EMS =10
            机器人:layout_weight =1
        安卓的inputType =手机/&GT;    &LT;的EditText
        机器人:ID =@ + ID / editText5
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID /按钮8
        机器人:layout_alignBottom =@ + ID /按钮8
        机器人:layout_alignParentRight =真
        机器人:EMS =10
            机器人:layout_weight =1
        安卓的inputType =textPersonName/&GT;    &LT;单选
        机器人:ID =@ + ID / radioButton2
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID /按钮9
        机器人:layout_alignBottom =@ + ID /按钮9
        机器人:layout_alignLeft =@ + ID / editText5
            机器人:layout_weight =1
        机器人:文字=男/&GT;    &LT;单选
        机器人:ID =@ + ID / radioButton1
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID / radioButton2
        机器人:layout_alignBottom =@ + ID / radioButton2
        机器人:layout_alignParentRight =真
            机器人:layout_weight =1
        机器人:文字=女/&GT;    &LT;按钮
        机器人:ID =@ + ID / button10
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ + ID /按钮9
        机器人:layout_marginTop =31dp
            机器人:layout_weight =1
        机器人:文字=按钮/&GT;    &LT;微调
        机器人:ID =@ + ID / spinDept
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID / button10
        机器人:layout_alignBottom =@ + ID / button10
        机器人:layout_alignLeft =@ + ID / radioButton2
        机器人:layout_alignParentRight =真
        机器人:layout_weight =1
        机器人:背景=#ff009999/&GT;
   &LT; / LinearLayout中&GT;
&LT; / RelativeLayout的&GT;


解决方案

这是如果你precisely一半需要一个很好的解决方法是,使用RelativeLayout的时候。你是什​​么,使家长为中心的假无形的观点,并左键是alignParentLeft和alignRight到假冒视图和右侧的按钮(反之亦然)

 &LT; RelativeLayout的
   机器人:layout_width =FILL_PARENT
   机器人:layout_height =WRAP_CONTENT&GT;   &LT;查看机器人:ID =@ + ID / fakeView
       机器人:layout_width =0dp
       机器人:layout_height =0dp
       机器人:layout_centerInParent =真/&GT;
   &LT;按钮
       机器人:layout_width =WRAP_CONTENT
       机器人:layout_height =WRAP_CONTENT
       机器人:layout_alignRight =@ ID / fakeView
       机器人:layout_alignParentLeft =真
       机器人:文字=左/&GT;   &LT;按钮
      机器人:layout_width =WRAP_CONTENT
      机器人:layout_height =WRAP_CONTENT
      机器人:layout_alignLeft =@ ID / fakeView
      机器人:layout_alignParentRight =真
      机器人:文字=右/&GT;&LT; / RelativeLayout的&GT;

它真的聪明。

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>

解决方案

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

这篇关于调整宽度半屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆