多布局:匹配父级、适合内容、调整大小
Multi layouts: match parent, fit content, adjust size
我想做这个但是我得不到我想要的...
这是我的代码:
<LinearLayout style="@style/currently_playing_box">
<TextView
android:text="@string/currently_playing"
style="@style/currently_playing" />
<android.support.constraint.ConstraintLayout style="@style/currently_playing_infos">
<TextView
android:id="@+id/current_artist"
android:text="Test"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="@id/current_radio"
style="@style/radio_artist" />
<TextView
android:text="Test"
android:id="@+id/current_title"
app:layout_constraintTop_toBottomOf="@id/current_artist"
app:layout_constraintEnd_toStartOf="@id/current_radio"
style="@style/radio_title" />
<ImageView
android:id="@+id/current_radio"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:adjustViewBounds="true"
app:layout_constraintEnd_toEndOf="parent"
android:src="@drawable/radio_button"/>
</android.support.constraint.ConstraintLayout>
</LinearLayout>
我无法让所有条件同时起作用
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:paddingLeft="15dp"
android:text="Text ONE"
android:textSize="16sp"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/current_artist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:paddingLeft="15dp"
android:text="Text TWO"
android:textSize="16sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/current_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:paddingLeft="15dp"
android:text="Text THREE"
android:textSize="16sp"
android:textStyle="bold"/>
</LinearLayout>
<ImageView
android:id="@+id/current_radio"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_weight="0.4"
android:adjustViewBounds="true"
android:src="@drawable/mprofilelogo"/>
</LinearLayout>
</LinearLayout>
请检查此 XML 代码。
希望这会奏效...
您正在混合 LinearLayout
和 ConstraintLayout
。虽然这是允许的,但您可以只使用 ConstraintLayout
来做您想做的事,这将简化您的布局。这是一个 mock-up:
我添加了背景颜色以显示小部件的范围。您似乎也从样式中获取了宽度和高度属性;我已经把它们说清楚了。
我使用了 match_constraints
(布局中的 0dp
),这是 ConstraintLayout
中的一个强大概念。 Here 是关于小部件约束的官方文档,有助于解释正在发生的事情。
这是上图的 XML:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_light"
android:text="Currently playing Currently playing Currently playing Currently playing Currently playing Currently playing Currently playing Currently playing Currently playing Currently playing Currently playing Currently playing "
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/current_artist"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:background="@android:color/holo_blue_light"
android:text="Current artist Current artist Current artist Current artist Current artist Current artist Current artist Current artist Current artist Current artist Current artist Current artist Current artist "
app:layout_constraintEnd_toStartOf="@+id/current_radio"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/current_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginTop="16dp"
android:background="@android:color/holo_green_light"
android:text="Current title Current title Current title Current title Current title Current title Current title Current title Current title Current title Current title Current title Current title "
app:layout_constraintEnd_toStartOf="@+id/current_radio"
app:layout_constraintStart_toStartOf="@+id/current_artist"
app:layout_constraintTop_toBottomOf="@+id/current_artist"
tools:ignore="HardcodedText" />
<ImageView
android:id="@+id/current_radio"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginEnd="16dp"
android:background="@android:color/holo_orange_dark"
android:adjustViewBounds="true"
android:src="@drawable/ic_launcher_foreground"
app:layout_constraintBottom_toBottomOf="@+id/current_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/current_artist"
tools:ignore="ContentDescription" />
</android.support.constraint.ConstraintLayout>
我想做这个但是我得不到我想要的...
这是我的代码:
<LinearLayout style="@style/currently_playing_box">
<TextView
android:text="@string/currently_playing"
style="@style/currently_playing" />
<android.support.constraint.ConstraintLayout style="@style/currently_playing_infos">
<TextView
android:id="@+id/current_artist"
android:text="Test"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="@id/current_radio"
style="@style/radio_artist" />
<TextView
android:text="Test"
android:id="@+id/current_title"
app:layout_constraintTop_toBottomOf="@id/current_artist"
app:layout_constraintEnd_toStartOf="@id/current_radio"
style="@style/radio_title" />
<ImageView
android:id="@+id/current_radio"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:adjustViewBounds="true"
app:layout_constraintEnd_toEndOf="parent"
android:src="@drawable/radio_button"/>
</android.support.constraint.ConstraintLayout>
</LinearLayout>
我无法让所有条件同时起作用
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:paddingLeft="15dp"
android:text="Text ONE"
android:textSize="16sp"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/current_artist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:paddingLeft="15dp"
android:text="Text TWO"
android:textSize="16sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/current_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:paddingLeft="15dp"
android:text="Text THREE"
android:textSize="16sp"
android:textStyle="bold"/>
</LinearLayout>
<ImageView
android:id="@+id/current_radio"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_weight="0.4"
android:adjustViewBounds="true"
android:src="@drawable/mprofilelogo"/>
</LinearLayout>
</LinearLayout>
请检查此 XML 代码。
希望这会奏效...
您正在混合 LinearLayout
和 ConstraintLayout
。虽然这是允许的,但您可以只使用 ConstraintLayout
来做您想做的事,这将简化您的布局。这是一个 mock-up:
我添加了背景颜色以显示小部件的范围。您似乎也从样式中获取了宽度和高度属性;我已经把它们说清楚了。
我使用了 match_constraints
(布局中的 0dp
),这是 ConstraintLayout
中的一个强大概念。 Here 是关于小部件约束的官方文档,有助于解释正在发生的事情。
这是上图的 XML:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_light"
android:text="Currently playing Currently playing Currently playing Currently playing Currently playing Currently playing Currently playing Currently playing Currently playing Currently playing Currently playing Currently playing "
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/current_artist"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:background="@android:color/holo_blue_light"
android:text="Current artist Current artist Current artist Current artist Current artist Current artist Current artist Current artist Current artist Current artist Current artist Current artist Current artist "
app:layout_constraintEnd_toStartOf="@+id/current_radio"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/current_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginTop="16dp"
android:background="@android:color/holo_green_light"
android:text="Current title Current title Current title Current title Current title Current title Current title Current title Current title Current title Current title Current title Current title "
app:layout_constraintEnd_toStartOf="@+id/current_radio"
app:layout_constraintStart_toStartOf="@+id/current_artist"
app:layout_constraintTop_toBottomOf="@+id/current_artist"
tools:ignore="HardcodedText" />
<ImageView
android:id="@+id/current_radio"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginEnd="16dp"
android:background="@android:color/holo_orange_dark"
android:adjustViewBounds="true"
android:src="@drawable/ic_launcher_foreground"
app:layout_constraintBottom_toBottomOf="@+id/current_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/current_artist"
tools:ignore="ContentDescription" />
</android.support.constraint.ConstraintLayout>