以声明方式将宽度指定为可用屏幕宽度的一半

bug*_*gzy 105 android android-widget

是否可以将窗口小部件宽度指定为可用屏幕宽度的一半,并使用声明性xml进行操作?

syn*_*nic 262

如果您的小部件是按钮:

<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>
Run Code Online (Sandbox Code Playgroud)

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

  • 最好为两个子元素使用android:layout_width ="0dp",避免两次调整大小. (14认同)
  • 我从来没有得到你必须声明layout_width ="0dp"的原因 (2认同)

Sur*_*gch 36

使用约束布局

  1. 添加指南
  2. 将百分比设置为50%
  3. 将您的视图约束到指南和父级.

在此输入图像描述

如果您在将其更改为百分比时遇到问题,请参阅此答案.

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>
Run Code Online (Sandbox Code Playgroud)

  • 这应该是最好的答案。 (3认同)

Raj*_*tth 15

将宽度设为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>
Run Code Online (Sandbox Code Playgroud)


小智 5

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

<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>
Run Code Online (Sandbox Code Playgroud)