簡體   English   中英

Layout_alignParentRight 和其他人不工作

[英]Layout_alignParentRight and others aren't working

我只是想通過制作生日賀卡應用程序來完成 Udacity 上的第一堂應用程序制作課程。 它要求我將“來自你的名字”放在生日賀卡的右下角。 我嘗試使用android:layout_alignParentRightandroid:layout_alignParentBottom並且沒有任何改變。

<?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:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Happy Birthday friendName!"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:text="From yourName!"
        android:layout_alignParentEnd="true" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/androidparty" />

如果你想使用android:layout_alignParentRightandroid:layout_alignParentBottom那么

使用RelativeLayout作為根布局

示例代碼

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:context=".MainActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Happy Birthday friendName!"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:text="From yourName!"
            android:layout_alignParentEnd="true" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/androidparty" />
</RelativeLayout>

當父布局為RelativeLayout時使用的android:layout_alignParentBottomandroid:layout_alignParentRight屬性,這兩個屬性不適用於ConstraintLayout

只需將您的根布局更改為RelativeLayout它將起作用

示例代碼

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:context=".MainActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Happy Birthday friendName!"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:text="From yourName!"
            android:layout_alignParentEnd="true" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/androidparty" />
</RelativeLayout>

android:layout_alignParentRightandroid:layout_alignParentBottom與 RelativeLayout 一起使用

對於約束布局,您必須使用

app:layout_constraintRight_toRightOf="parent"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM