Android:如何将TextView与上下元素对齐?

时间:2017-01-06 07:40:19

标签: android android-layout android-linearlayout textview android-scrollview

请查看以下用户界面(请忽略红色评论)

enter image description here

以下是我的尝试

<?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:id="@+id/content_id_target_ranges"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".activities.TargetRangesActivity"
    tools:showIn="@layout/app_bar_target_ranges">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

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

            <TextView
                android:id="@+id/target_ranges_title_txt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="12dp"
                android:text="@string/my_settings_target_ranges"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/target_ranges_sub_heading_txt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="6dp"
                android:text="@string/target_ranges_sub_heading"
                android:textSize="14sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="12dp"
                android:background="@drawable/linear_border"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/target_ranges_blood_glucose_txt"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"
                    android:layout_marginTop="12dp"
                    android:text="@string/target_ranges_blood_glucose_text"
                    android:textSize="16sp" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginRight="15dp"
                        android:layout_marginTop="6dp"
                        android:layout_weight="1"
                        android:orientation="vertical">

                        <TextView
                            android:id="@+id/target_ranges_blood_glucose_pre_meal_txt"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/target_ranges_blood_glucose_pre_meal"
                            android:textAlignment="center"
                            android:textSize="12sp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/target_ranges_blood_glucose_pre_meal_unit_txt"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="12dp"
                            android:text="@string/target_ranges_blood_glucose_unit"
                            android:textAlignment="center"
                            android:textSize="10sp" />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="5dp"
                        android:layout_marginTop="6dp"
                        android:layout_weight="1">

                        <android.support.design.widget.TextInputLayout
                            android:layout_width="48dp"
                            android:layout_height="48dp"
                            android:layout_marginBottom="5dp"
                            android:layout_marginLeft="5dp"
                            android:layout_marginRight="5dp"
                            android:layout_marginTop="5dp"
                            android:background="@drawable/edit_text_style">

                            <EditText
                                android:id="@+id/target_ranges_blood_glucose_first_edit"
                                style="@style/EditTextStyle"
                                android:focusable="true" />

                        </android.support.design.widget.TextInputLayout>


                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="6dp"
                        android:layout_weight="1">

                        <android.support.design.widget.TextInputLayout
                            android:layout_width="48dp"
                            android:layout_height="48dp"
                            android:layout_marginBottom="5dp"
                            android:layout_marginLeft="5dp"
                            android:layout_marginRight="5dp"
                            android:layout_marginTop="5dp"
                            android:background="@drawable/edit_text_style">

                            <EditText
                                android:id="@+id/target_ranges_blood_glucose_second_edit"
                                style="@style/EditTextStyle"
                                android:focusable="true" />

                        </android.support.design.widget.TextInputLayout>

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="6dp"
                        android:layout_weight="1">

                        <android.support.design.widget.TextInputLayout
                            android:layout_width="48dp"
                            android:layout_height="48dp"
                            android:layout_marginBottom="5dp"
                            android:layout_marginLeft="5dp"
                            android:layout_marginRight="5dp"
                            android:layout_marginTop="5dp"
                            android:background="@drawable/edit_text_style">

                            <EditText
                                android:id="@+id/target_ranges_blood_glucose_third_edit"
                                style="@style/EditTextStyle"
                                android:focusable="true" />

                        </android.support.design.widget.TextInputLayout>

                    </LinearLayout>

                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="12dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="30dp"
                        android:orientation="horizontal"
                        android:weightSum="1">

                        <TextView
                            android:layout_width="34dp"
                            android:layout_height="48dp"
                            android:textSize="16sp"
                            android:textColor="@color/colorWhite"
                            android:background="@drawable/gray_colour_style"
                            android:layout_weight="1.46" />

                        <TextView
                            android:layout_width="90dp"
                            android:layout_height="48dp"
                            android:textSize="16sp"
                            android:background="@drawable/green_colour_style"
                            android:textColor="@color/colorWhite"
                            android:layout_weight="0.15" />

                        <TextView
                            android:layout_width="60dp"
                            android:layout_height="48dp"
                            android:textSize="16sp"
                            android:background="@drawable/yellow_colour_style"
                            android:textColor="@color/colorWhite" />

                        <TextView
                            android:layout_width="70dp"
                            android:layout_height="48dp"
                            android:textSize="16sp"
                            android:background="@drawable/red_colour_style"
                            android:textColor="@color/colorWhite"/>

                    </LinearLayout>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"
                    android:layout_marginTop="12dp"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginRight="10dp"
                        android:layout_marginTop="6dp"
                        android:layout_weight="1"
                        android:orientation="vertical">

                        <TextView
                            android:id="@+id/target_ranges_blood_glucose_post_meal_txt"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/target_ranges_blood_glucose_post_meal"
                            android:textAlignment="center"
                            android:textSize="12sp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/target_ranges_blood_glucose_post_meal_unit_txt"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="12dp"
                            android:text="@string/target_ranges_blood_glucose_unit"
                            android:textAlignment="center"
                            android:textSize="10sp" />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="5dp"
                        android:layout_marginTop="6dp"
                        android:layout_weight="1">

                        <android.support.design.widget.TextInputLayout
                            android:layout_width="48dp"
                            android:layout_height="48dp"
                            android:layout_marginBottom="5dp"
                            android:layout_marginLeft="5dp"
                            android:layout_marginRight="5dp"
                            android:layout_marginTop="5dp"
                            android:background="@drawable/edit_text_style">

                            <EditText
                                android:id="@+id/target_ranges_blood_glucose_four_edit"
                                style="@style/EditTextStyle"
                                android:focusable="true" />

                        </android.support.design.widget.TextInputLayout>


                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="6dp"
                        android:layout_weight="1">

                        <android.support.design.widget.TextInputLayout
                            android:layout_width="48dp"
                            android:layout_height="48dp"
                            android:layout_marginBottom="5dp"
                            android:layout_marginLeft="5dp"
                            android:layout_marginRight="5dp"
                            android:layout_marginTop="5dp"
                            android:background="@drawable/edit_text_style">

                            <EditText
                                android:id="@+id/target_ranges_blood_glucose_five_edit"
                                style="@style/EditTextStyle"
                                android:focusable="true" />

                        </android.support.design.widget.TextInputLayout>

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="6dp"
                        android:layout_weight="1">

                        <android.support.design.widget.TextInputLayout
                            android:layout_width="48dp"
                            android:layout_height="48dp"
                            android:layout_marginBottom="5dp"
                            android:layout_marginLeft="5dp"
                            android:layout_marginRight="5dp"
                            android:layout_marginTop="5dp"
                            android:background="@drawable/edit_text_style">

                            <EditText
                                android:id="@+id/target_ranges_blood_glucose_six_edit"
                                style="@style/EditTextStyle"
                                android:focusable="true" />

                        </android.support.design.widget.TextInputLayout>

                    </LinearLayout>

                </LinearLayout>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="24dp" />

            </LinearLayout>



        </LinearLayout>

    </ScrollView>

</RelativeLayout>

然而,它生成了以下UI

enter image description here

注意彩色条如何与上下文本框没有正确对齐,因为第一个图像是通过虚线指示的(虚线表示每个彩色瓷砖边缘的位置,与文本框对齐)

另请注意我的文本框与预期的不同。

如何将其显示在第一张图片中?

1 个答案:

答案 0 :(得分:1)

在这里,我修复了你的内部布局,更改了drawable和color ..等等

<?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:id="@+id/content_id_target_ranges"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/down_shadow"
        android:orientation="vertical"
        android:weightSum="20">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:text="Texttttttttttttttttttttttttttttttttt"
            android:textColor="#000" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3"
            android:orientation="horizontal"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:weightSum="10">

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/pre_meal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="kkk lkjl" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/pre_meal"
                    android:gravity="center"
                    android:text="sd/sd" />

            </RelativeLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="8"
                android:orientation="horizontal"
                android:weightSum="6">


                <EditText
                    android:id="@+id/one"
                    android:layout_width="0dp"
                    android:layout_height="48dp"
                    android:layout_weight="1"
                    android:background="#999698"
                    android:focusable="true" />

                <View
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1" />

                <EditText
                    android:id="@+id/two"
                    android:layout_width="0dp"
                    android:layout_height="48dp"
                    android:layout_weight="1"
                    android:background="#999698"
                    android:focusable="true" />


                <View
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1" />


                <EditText
                    android:id="@+id/three"
                    android:layout_width="0dp"
                    android:layout_height="48dp"
                    android:layout_weight="1"
                    android:background="#999698"
                    android:focusable="true" />

                <View
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1" />

            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:orientation="horizontal"
            android:weightSum="9">

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="48dp"
                android:layout_weight="1.5"
                android:background="#8c9393"
                android:gravity="center"
                android:text="Low"
                android:textColor="#fff" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="48dp"
                android:layout_weight="2.4"
                android:background="#1dea12"
                android:gravity="center"
                android:text="Normal"
                android:textColor="#fff" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="48dp"
                android:layout_weight="2.2"
                android:background="#f5d904"
                android:gravity="center"
                android:text="High"
                android:textColor="#fff" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="48dp"
                android:layout_weight="1.9"
                android:background="#ea0c05"
                android:gravity="center"
                android:layout_marginRight="2dp"
                android:text="Very High"
                android:textColor="#fff" />


        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3"
            android:orientation="horizontal"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:weightSum="10">

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/pre_meals"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="kkk lkjl" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/pre_meals"
                    android:gravity="center"
                    android:text="sd/sd" />

            </RelativeLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="8"
                android:orientation="horizontal"
                android:weightSum="6">


                <EditText
                    android:id="@+id/four"
                    android:layout_width="0dp"
                    android:layout_height="48dp"
                    android:layout_weight="1"
                    android:background="#999698"
                    android:focusable="true" />

                <View
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1" />


                <EditText
                    android:id="@+id/five"
                    android:layout_width="0dp"
                    android:layout_height="48dp"
                    android:layout_weight="1"
                    android:background="#999698"
                    android:focusable="true" />

                <View
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1" />

                <EditText
                    android:id="@+id/six"
                    android:layout_width="0dp"
                    android:layout_height="48dp"
                    android:layout_weight="1"
                    android:background="#999698"
                    android:focusable="true" />

                <View
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1" />

            </LinearLayout>

        </LinearLayout>


    </LinearLayout>

</RelativeLayout>

使用TextInput布局而不是Edittext。编译时出错,所以我将其更改为Edittext