【问题标题】:Android XML Layout - percentage + wrap_contentAndroid XML 布局 - 百分比 + wrap_content
【发布时间】:2013-12-28 14:42:16
【问题描述】:

我想做一些布局的一部分,比如 ex.:

[_____EditText1_____] / [_____EditText2_____]

我不想在 dp/px 等中设置宽度。 我想为“斜线”符号“/”包装内容的宽度,并将剩余宽度除以 EditText1 和 EditText2 的(50% 到 50%)。 很简单,只有EditText1和EditText2,两个都设置就好了:

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

但是如何在两个EditTexts之间设置TextView“/”和宽度wrap_content?

【问题讨论】:

    标签: android xml layout


    【解决方案1】:
    <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>
    

    【讨论】:

      【解决方案2】:

      试试这个:

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

      【讨论】:

        【解决方案3】:

        你可以试试这个

        <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 将占用它的空间,剩余的将平均分配给EditViews。

        希望这会有所帮助。

        【讨论】:

          【解决方案4】:
          <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>
          

          关键是增加layout_weight中间元素的值;)

          【讨论】:

            猜你喜欢
            • 2011-09-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-10-18
            • 1970-01-01
            • 2015-11-20
            • 2013-05-19
            • 1970-01-01
            相关资源
            最近更新 更多