如何在Android中进行线性布局设计?

如何在Android中进行线性布局设计?,android,android-linearlayout,textview,Android,Android Linearlayout,Textview,我需要在图片中做一个这样的布局: 这里有一个根据微调器值变化的国家,还有一个编辑文本必须放置手机号码的地方。编辑文本的格式必须为-。国家代码从API中提取,数字由用户输入。但是,它应该仅显示为一个EditText,而不是TextView和EditText在LinearLayout中 我怎样才能做到这一点 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="htt

我需要在图片中做一个这样的布局:

这里有一个根据
微调器
值变化的国家,还有一个
编辑文本
必须放置手机号码的地方。编辑文本的格式必须为-。国家代码从API中提取,数字由用户输入。但是,它应该仅显示为一个
EditText
,而不是
TextView
EditText
LinearLayout

我怎样才能做到这一点

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:fb="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/common_bg"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context="com.pigeonmarket.PMRegisterActivity">

    <!-- Login progress -->
    <ProgressBar


        android:id="@+id/login_progress"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:visibility="gone" />


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/registerform"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="20dp">

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/toolbar_new"
                android:layout_centerHorizontal="true"
                android:text="REGISTER"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#fff"
                android:textSize="18dp" />

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_centerHorizontal="true"
                android:layout_marginTop="40dp"
                android:background="@drawable/signup_icon" />



            <EditText
                android:id="@+id/sname"
                android:layout_width="match_parent"
                android:layout_height="60dp"

                android:layout_marginTop="14dp"

                android:hint="Name"
                android:paddingLeft="15dp"
                android:textSize="14dp"
                android:layout_below="@+id/imageView"
                android:layout_centerHorizontal="true" />

            <Spinner
                android:layout_width="match_parent"
                android:layout_height="60dp"
                style="?android:attr/spinnerItemStyle"
                android:id="@+id/fbcountry"

                android:paddingLeft="15dp"
                android:layout_marginTop="12dp"
                android:drawableRight="@drawable/down_arrow"
                android:drawablePadding="15dp"
                android:paddingRight="15dp"
                android:spinnerMode="dialog"
                android:layout_below="@+id/sname"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true" />





            <TextView

            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:paddingLeft="35dp"
            android:layout_below="@+id/fbcountry"
            android:layout_alignR="@+id/fbcountry"
            android:hint="+"
            android:layout_marginTop="30dp"
            android:textSize="14dp"/>


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:paddingLeft="50dp"
                android:layout_below="@+id/fbcountry"
                android:layout_alignR="@+id/fbcountry"
                android:hint="code"
                android:layout_marginTop="30dp"
                android:textSize="14dp"
                android:id="@+id/calling_code" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:paddingLeft="90dp"
                android:layout_below="@+id/fbcountry"
                android:layout_alignR="@+id/fbcountry"
                android:hint="Mobile Number"
                android:layout_marginTop="10dp"
                android:textSize="14dp"
                android:id="@+id/mob" />





        </RelativeLayout>

    </ScrollView>

</LinearLayout>

在drawable中创建background.xml并将其添加为编辑文本和微调器的背景

有几种方法可以实现这一点

  • 创建一个
    LinearLayout
    ,该布局具有要放入
    微调器和其他
    编辑文本的背景。为其提供水平方向,并添加一个
    TextView
    (包含API中的国家代码)和一个
    EditText
    ,用户可以在其中输入数字(最好使用
    weight
    =1,且无背景)。这将以一种简单的方式实现同样的目标
  • 在代码中创建一个名为
    countryCode
    字符串
    ,将API中的国家代码存储在此。将
    TextWatcher
    添加到
    EditText
    中,并在输入中添加前缀
    countryCode
    注意:不建议更改
    onTextChanged()
    回调中字段的文本,因为可能会触发infi循环

  • 我想你只需先将api值设置为编辑文本,然后,如果用户单击编辑文本字段,他可以在国家代码后继续键入他的手机号码,图片在哪里?好的,等等,我再次上传,再次查看我再次上传,但编辑文本高度宽度不等于喷丝头此代码将只创建您需要提供的背景布局中的宽度xmlgud选项..让我试试
    create background.xml in drawable & add this as background of edit text & spinner
    
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="#00000000"/> <-- transparent-->
        <stroke android:color="@color/grey" <-- grey color-->
            android:width="10dp"/>
        <corners
            android:bottomLeftRadius="30dp"
            android:bottomRightRadius="30dp"
            android:topLeftRadius="30dp"
            android:topRightRadius="30dp"/>
    </shape>