【问题标题】:Assign width to half available screen width declaratively以声明方式将宽度分配给可用屏幕宽度的一半
【发布时间】:2011-02-04 14:32:41
【问题描述】:

是否可以将小部件宽度分配为可用屏幕宽度的一半,并使用声明性 xml 来实现?

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

    如果您的小部件是按钮:

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        android:orientation="horizontal">
        <Button android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="somebutton"/>
    
        <TextView android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
    </LinearLayout>
    

    我假设您希望您的小部件占据一半,而另一个小部件占据另一半。诀窍是使用LinearLayout,在两个小部件上设置layout_width="fill_parent",并将layout_weight 在两个小部件上设置为相同的值。如果有两个小部件,它们的权重相同,LinearLayout 将在两个小部件之间分割宽度。

    【讨论】:

    • 最好对两个子元素使用 android:layout_width="0dp",避免两次调整大小。
    • 我不明白为什么你必须声明 layout_width="0dp"
    • 你也可以在安卓以后的版本中使用作为填充符。如果您只是打算将其用作填充物,我认为 View 比 TextView 轻一点。 layout_width="0dp" 实际上是根据Android文档推荐的方法。
    • 效果很好!非常感谢!!
    • @Andrew:因为这种方式布局渲染器不会尝试使用组件的layout_width,它会直接跳到根据权重共享额外的宽度。
    【解决方案2】:

    使用约束布局

    1. 添加指南
    2. 将百分比设置为 50%
    3. 将您的视图限制在指南和父级上。

    如果您在将其更改为百分比时遇到问题,请参阅this answer

    XML

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="81dp">
    
        <android.support.constraint.Guideline
            android:id="@+id/guideline8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.5"/>
    
        <TextView
            android:id="@+id/textView6"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:text="TextView"
            app:layout_constraintBottom_toTopOf="@+id/guideline8"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
        
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

    • 这应该是最好的答案了。
    • 我是 android 新手,我为我的问题找到的几乎每个微调器答案都使用了我在 Android Studio 的 Legacy 部分中看到的 RelativeLayout。您的回答简单、快速且非常用户友好。非常非常感谢。
    【解决方案3】:

    将宽度设为 0dp 以确保其大小与重量完全一致 这将确保即使子视图的内容变大,它们仍然会被限制在一半(根据重量)

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1"
         >
    
        <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="click me"
        android:layout_weight="0.5"/>
    
    
        <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:layout_weight="0.5"/>
      </LinearLayout>
    

    【讨论】:

    • 我认为 android:layout_width="0dp" 是正确的,但是将每个权重设置为 0.5 并将 weitghtSum 设置为它们的总和是不需要的。似乎您只需要在两个子视图上具有相同的权重..
    【解决方案4】:

    居中单个项目的另一种方式,占据屏幕的一半:

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <View
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:visibility="invisible" />
    
            <EditText
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2" />
    
           <View
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:visibility="invisible" />
    
    </LinearLayout>
    

    【讨论】:

      【解决方案5】:
      <LinearLayout 
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" >
      <TextView
          android:id="@+id/textD_Author"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="20dp"
          android:text="Author : "
          android:textColor="#0404B4"
          android:textSize="20sp" />
       <TextView
          android:id="@+id/textD_Tag"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginTop="20dp"
          android:text="Edition : "
          android:textColor="#0404B4"
          android:textSize="20sp" />
      <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="horizontal"
          android:weightSum="1" >
          <Button
              android:id="@+id/btbEdit"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="0.5"
              android:text="Edit" />
          <Button
              android:id="@+id/btnDelete"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="0.5"
              android:text="Delete" />
      </LinearLayout>
      </LinearLayout>
      

      【讨论】:

      • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多