简体   繁体   English

Android XML布局-百分比+ wrap_content

[英]Android XML Layout - percentage + wrap_content

I want to make some part of layout like ex.: 我想做一些像ex。这样的布局:

[_____EditText1_____] / [_____EditText2_____]

and I didn't want to set width in dp/px etc. I want to wrap_content fo width for 'slash' sign "/" and divide remaining width for (50% to 50%) for EditText1 and EditText2. 并且我不想在dp / px等中设置宽度。我想将'slash'符号“ /”的wrap_content fo宽度和EditText1和EditText2的剩余宽度除以(50%到50%)。 It's simple for only EditText1 and EditText2, just setting in both of them: 仅对EditText1和EditText2进行设置就很简单,只需对它们进行设置即可:

android:layout_width="0dp"
android:layout_weight=".5"

but how to set TextView "/" and for width wrap_content, between two EditTexts? 但是如何在两个EditText之间设置TextView“ /”并设置wrap_content的宽度?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
  android:layout_height="match_parent"
   >

   <EditText
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight=".5"
   />

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="/" />
 <EditText
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_weight=".5"
 />
</LinearLayout>

Try this: 尝试这个:

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.48" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="/"
        android:layout_weight="0.04" />

    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.48" />

</LinearLayout>

You can try this 你可以试试这个

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <EditText 
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"/>

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="/"/>

    <EditText 
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"/>

</LinearLayout>

TextView will occupy its space and the remainning will be distributed among EditView s equally. TextView将占据其空间,其余部分将平均分配给EditView

Hope this helps. 希望这可以帮助。

<LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="fill_parent">
        <TextView
            android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="0"
            android:gravity="center_vertical|center_horizontal" />
        <TextView
            android:layout_weight="1.5"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="/"
            android:gravity="center_vertical|center_horizontal" />
        <TextView
            android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="0"
            android:gravity="center_vertical|center_horizontal" />
   </LinearLayout>

The key is to increase layout_weight value of the middle element ;) 关键是增加中间元素的layout_weight值;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM