【问题标题】:Baseline Constraint in Linear, scrollable layouts线性可滚动布局中的基线约束
【发布时间】:2017-07-20 10:44:05
【问题描述】:

我需要确保两个视图元素彼此相邻且垂直高度相同。我会使用基线约束来做到这一点,但目前我正在使用线性、可滚动布局(滚动视图中的线性布局),当我单击一个元素时,它不允许我从中获取基线约束并连接它到其他地方。

XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:id="@+id/newOrderScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
tools:context="eof.concrete.NewOrderActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/activity_horizontal_margin"
    android:layout_marginBottom="@dimen/activity_horizontal_margin"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical"
    android:paddingBottom="16dp"
    android:baselineAligned="false">

    <TextView
        android:id="@+id/newOrderHeadlineTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Yeni Sipariş"
        android:textColor="@android:color/black"
        android:textSize="10pt"
        android:textStyle="bold"></TextView>

    <TextView
        android:id="@+id/materialCodeTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="Malzeme kodu"
        android:textSize="10pt"></TextView>

    <Spinner
        android:id="@+id/materialsSpinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></Spinner>

    <TextView
        android:id="@+id/amountTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:text="Miktar"
        android:textSize="10pt"></TextView>

    <EditText
        android:id="@+id/amountText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Örnek: 1000"
        android:inputType="numberDecimal" />

    <TextView
        android:id="@+id/dateTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="Teslim tarihi"
        android:textSize="10pt"></TextView>

    <EditText
        android:id="@+id/dateText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Örnek: 10/01/2017"
        android:inputType="date" />

    <TextView
        android:id="@+id/cityTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="İl"
        android:textSize="10pt"></TextView>

    <Spinner
        android:id="@+id/citiesSpinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></Spinner>

    <TextView
        android:id="@+id/provinceTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="İlçe"
        android:textSize="10pt"></TextView>

    <Spinner
        android:id="@+id/provincesSpinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></Spinner>

    <TextView
        android:id="@+id/priceTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Pompa fiyatı"
        android:textSize="10pt"></TextView>

    <EditText
        android:id="@+id/priceText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Örnek: 3.45"
        android:inputType="numberDecimal" />

    <TextView
        android:id="@+id/totalPriceTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Toplam fiyat: $$$"
        android:textSize="10pt"></TextView>

    <TextView
        android:id="@+id/paymentTypeTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Ödeme Şekli"
        android:textColor="@android:color/black"
        android:textSize="10pt"
        android:textStyle="bold"></TextView>

    <RadioGroup
        android:id="@+id/paymentTypeRadioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Peşin   "></RadioButton>

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/radioButton1"
            android:text="Kredi kartı   "></RadioButton>

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/radioButton2"
            android:text="Çek/Senet   "></RadioButton>
    </RadioGroup>

    <TextView
        android:id="@+id/authorizedTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="İrtibat yetkilisi"
        android:textSize="10pt"></TextView>

    <EditText
        android:id="@+id/authorizedText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Örnek: Ali Kayataş"
        android:inputType="textPersonName" />

    <TextView
        android:id="@+id/authorizedPhoneTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="Yetkili telefon no"
        android:textSize="10pt"></TextView>

    <EditText
        android:id="@+id/authorizedPhoneText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Örnek: 554 123 45 67"
        android:inputType="number" />

    <TextView
        android:id="@+id/facilityTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="Tesis"
        android:textSize="10pt"></TextView>

    <Spinner
        android:id="@+id/facilitiesSpinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></Spinner>

    <TextView
        android:id="@+id/explanationTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="Açıklama"
        android:textSize="10pt"></TextView>

    <EditText
        android:id="@+id/explanationText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:ems="10"
        android:gravity="left|top"
        android:inputType="textMultiLine"></EditText>

    <Button
        android:id="@+id/confirmNewOrderButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="32dp"
        android:layout_marginRight="32dp"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:background="@color/colorPrimary"
        android:text="Kaydet" />

</LinearLayout>

</ScrollView>

EditTexts 应该与 TextViews 在同一水平线上。

【问题讨论】:

  • 请分享您的 XML 代码
  • 我刚刚添加了它,先生。
  • IDE 标签不相关

标签: android user-interface xml-layout


【解决方案1】:

只需复制此代码并检查这是您想要的

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:id="@+id/newOrderScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/activity_horizontal_margin"
    android:layout_marginBottom="@dimen/activity_horizontal_margin"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical"
    android:paddingBottom="16dp"
    android:baselineAligned="false">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    <TextView
        android:id="@+id/newOrderHeadlineTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Yeni Sipariş"
        android:textColor="@android:color/black"
        android:textSize="10pt"
        android:textStyle="bold"></TextView>

    <TextView
        android:id="@+id/materialCodeTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="Malzeme kodu"
        android:textSize="10pt"></TextView>

    <Spinner
        android:id="@+id/materialsSpinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        ></Spinner>
        </LinearLayout>
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/amountTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:text="Miktar"
        android:textSize="10pt"></TextView>

    <EditText
        android:id="@+id/amountText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Örnek: 1000"
        android:inputType="numberDecimal"
        android:layout_marginTop="@dimen/activity_horizontal_margin"/>
    </LinearLayout>
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/dateTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="Teslim tarihi"
        android:textSize="10pt"></TextView>

    <EditText
        android:id="@+id/dateText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Örnek: 10/01/2017"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:inputType="date" />
    </LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

<TextView
        android:id="@+id/cityTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="İl"
        android:textSize="10pt"></TextView>

    <Spinner
        android:id="@+id/citiesSpinner"
        android:layout_width="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:layout_height="wrap_content"></Spinner></LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

<TextView
        android:id="@+id/provinceTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"

        android:text="İlçe"
        android:textSize="10pt"></TextView>

    <Spinner
        android:id="@+id/provincesSpinner"
        android:layout_width="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:layout_height="wrap_content"></Spinner></LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/priceTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Pompa fiyatı"
        android:textSize="10pt"></TextView>

    <EditText
        android:id="@+id/priceText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:hint="Örnek: 3.45"
        android:inputType="numberDecimal" /></LinearLayout>


<TextView
        android:id="@+id/totalPriceTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Toplam fiyat: $$$"
        android:textSize="10pt"></TextView>


<TextView
        android:id="@+id/paymentTypeTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Ödeme Şekli"
        android:textColor="@android:color/black"
        android:textSize="10pt"
        android:textStyle="bold"></TextView>

    <RadioGroup
        android:id="@+id/paymentTypeRadioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Peşin   "></RadioButton>

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/radioButton1"
            android:text="Kredi kartı   "></RadioButton>

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/radioButton2"
            android:text="Çek/Senet   "></RadioButton>
    </RadioGroup>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

<TextView
        android:id="@+id/authorizedTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="İrtibat yetkilisi"
        android:textSize="10pt"></TextView>

    <EditText
        android:id="@+id/authorizedText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:hint="Örnek: Ali Kayataş"
        android:inputType="textPersonName" /></LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

<TextView
        android:id="@+id/authorizedPhoneTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="Yetkili telefon no"
        android:textSize="10pt"></TextView>

    <EditText
        android:id="@+id/authorizedPhoneText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:hint="Örnek: 554 123 45 67"
        android:inputType="number" /></LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
<TextView
        android:id="@+id/facilityTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="Tesis"
        android:textSize="10pt"></TextView>

    <Spinner
        android:id="@+id/facilitiesSpinner"
        android:layout_width="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:layout_height="wrap_content"></Spinner></LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

<TextView
        android:id="@+id/explanationTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:text="Açıklama"
        android:textSize="10pt"></TextView>

    <EditText
        android:id="@+id/explanationText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:ems="10"
        android:gravity="left|top"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:inputType="textMultiLine"></EditText></LinearLayout>

    <Button
        android:id="@+id/confirmNewOrderButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="32dp"
        android:layout_marginRight="32dp"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:background="@color/colorPrimary"
        android:text="Kaydet" />

</LinearLayout>

</ScrollView>

【讨论】:

    【解决方案2】:

    我希望这是你想要的

    <ScrollView 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:id="@+id/newOrderScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal>
    
                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"/>
    
                    <EditText
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"/>
    
                </LinearLayout>
    
            </LinearLayout>
       </ScrollView>
    

    内部的 LinearLayout 确保它的 childLayouts 在水平方向上依次出现 1

    【讨论】:

      【解决方案3】:

      您说“基线约束”是指ConstraintLayout 的功能,但您使用的是LinearLayout

      如果您想使用基线约束,那么您应该将您的LinearLayout 转换为ConstraintLayout。然后,您可以在 XML 中使用 app:layout_constraintBaseline_toBaselineOf 约束基线。

      【讨论】:

        猜你喜欢
        • 2016-07-11
        • 1970-01-01
        • 2014-08-03
        • 1970-01-01
        • 2020-06-28
        • 1970-01-01
        • 1970-01-01
        • 2020-01-16
        • 2022-06-23
        相关资源
        最近更新 更多