【问题标题】:Give Scroll view to only edittext in android让滚动视图仅在 android 中编辑文本
【发布时间】:2017-07-22 07:39:27
【问题描述】:

需要只为edittext字段提供滚动视图,而不是为整个布局提供滚动 在这个布局中,我使用的是edittext,我只需要给scrollview那个edittext而不是所有布局 这是我的代码 See here is image

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbarforprofile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        android:minHeight="?attr/actionBarSize"
        android:titleTextColor="#ffffff"
        app:theme="@style/ToolbarColoredBackArrow" />


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


        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:layout_marginBottom="-80dp"
            android:layout_marginTop="16dp"
            android:elevation="2dp"
            android:padding="16dp"
            android:src="@drawable/electricle" />


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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="18dp"
                android:drawableLeft="@drawable/leftarrow"

                android:textColor="@color/demo"
                android:textSize="0sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="-65.5dp"
                android:layout_marginRight="18dp"
                android:layout_marginTop="18dp">


                <LinearLayout
                    android:id="@+id/profile_details"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/demo"

                        android:orientation="vertical"
                        android:padding="16dp">

                        <TextView
                            android:id="@+id/txtservicedetails"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:layout_marginBottom="20dp"
                            android:layout_marginTop="40dp"
                            android:gravity="center"
                            android:text="@string/app_name"
                            android:textColor="#fff"
                            android:textSize="20sp"
                            android:textStyle="bold" />

                        <android.support.v4.widget.NestedScrollView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            app:layout_behavior="@string/appbar_scrolling_view_behavior">

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


                                <EditText
                                    android:id="@+id/editText_requirement"
                                    android:layout_width="match_parent"
                                    android:layout_height="70dp"
                                    android:layout_margin="3dp"
                                    android:background="#fff"
                                    android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "
                                    android:gravity="top"

                                    android:hint="Your Requirement (optional)"
                                    android:inputType="textMultiLine|textCapSentences"
                                    android:padding="6dp"
                                    android:textColor="#000" />

                                <DatePicker
                                    android:id="@+id/datePicker"
                                    android:layout_width="wrap_content"
                                    android:layout_height="match_parent"
                                    android:visibility="gone" />

                                <EditText
                                    android:id="@+id/et_dateof_visit"
                                    android:layout_width="match_parent"
                                    android:layout_height="40dp"
                                    android:layout_margin="5dp"

                                    android:background="#fff"
                                    android:focusable="false"
                                    android:hint="Date Of Visit"
                                    android:inputType="date"
                                    android:maxLines="1"
                                    android:padding="5dp"
                                    android:textColor="#000" />

                                <EditText
                                    android:id="@+id/et_time"
                                    android:layout_width="match_parent"
                                    android:layout_height="40dp"
                                    android:layout_margin="5dp"
                                    android:background="#fff"
                                    android:focusable="false"
                                    android:hint="Time Of Visit"
                                    android:inputType="time"
                                    android:maxLines="1"
                                    android:padding="5dp"
                                    android:textColor="#000" />

                                <EditText

                                    android:id="@+id/et_noofperson"
                                    android:layout_width="match_parent"
                                    android:layout_height="40dp"
                                    android:layout_margin="5dp"
                                    android:background="#fff"
                                    android:focusable="true"
                                    android:hint="No Of Person You Need"
                                    android:inputType="number"
                                    android:maxLength="2"
                                    android:maxLines="1"
                                    android:padding="5dp"
                                    android:singleLine="true"
                                    android:text="" />

                                <EditText

                                    android:id="@+id/et_serviceaddress"
                                    android:layout_width="match_parent"
                                    android:layout_height="70dp"
                                    android:layout_margin="5dp"
                                    android:background="#fff"
                                    android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890/-,: "
                                    android:focusable="true"
                                    android:gravity="top"
                                    android:hint="Service Address"
                                    android:inputType="textMultiLine|textCapSentences"
                                    android:maxLines="4"
                                    android:padding="10dp"
                                    android:singleLine="true"
                                    android:text="" />


                            </LinearLayout>
                        </android.support.v4.widget.NestedScrollView>


                    </LinearLayout>

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginBottom="20dp"
                        android:layout_marginTop="-20dp"
                        android:gravity="center">

                        <Button
                            android:id="@+id/btn_submit"
                            style="@style/Widget.AppCompat.Button.Colored"
                            android:layout_width="wrap_content"
                            android:layout_height="45dp"
                            android:layout_gravity="center"
                            android:layout_marginTop="20dp"
                            android:background="@drawable/rounded_green"
                            android:ems="10"
                            android:gravity="center"
                            android:text="Submit"
                            android:textSize="20dp"
                            android:textStyle="bold" />

                        <ProgressBar
                            android:id="@+id/btnsubmit"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_centerInParent="true"
                            android:layout_gravity="center"
                            android:visibility="gone" />

                    </RelativeLayout>


                </LinearLayout>


            </LinearLayout>

        </LinearLayout>


    </LinearLayout>

</LinearLayout>

【问题讨论】:

  • 你根本不清楚自己想要什么
  • 我只想将滚动视图提供给edittext字段,而不是提交按钮和第一个文本视图
  • 在您编辑问题并添加图表或图像来解释您的问题之前,此问题将继续收到反对票
  • 还添加了一张图片

标签: android xml android-layout


【解决方案1】:

试试下面这样的东西,

 View.OnTouchListener touchListener = new View.OnTouchListener() 
{
        public boolean onTouch(final View v, final MotionEvent motionEvent) 
{
            v.getParent().requestDisallowInterceptTouchEvent(true);
            switch (motionEvent.getAction() & MotionEvent.ACTION_MASK)
 {
                case MotionEvent.ACTION_UP:
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                    break;
            }
            return false;
        }
    };

希望对你有帮助,祝你一切顺利。

【讨论】:

    【解决方案2】:

    我仍然不确定我是否正确理解了您的问题。我假设您想让EditText(s) 滚动,同时保持顶部TextView 和底部Button 固定到位。

    试试这个。我将首先将LinearLayout 作为主容器,然后添加TextViewScrollViewButton 作为子布局。

    然后所有需要滚动的EditText(s) 都可以添加到LinearLayout,作为子级添加到ScrollView

    查看此代码

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:card_view="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbarforprofile"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimaryDark"
            android:minHeight="?attr/actionBarSize"
            android:titleTextColor="#ffffff"/>
    
        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:layout_marginTop="16dp"
            android:layout_marginBottom="16dp"/>
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:text="@string/app_name"/>
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                <!--Put all your scrollable content in here-->
    
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
    
            </LinearLayout>
    
        </ScrollView>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_gravity="center"/>
    
    </LinearLayout>
    

    我已将layout_weight 属性赋予ScrollView,以确保Button 始终位于底部。

    PS:如果这不是您需要的解决方案,请发表评论。

    【讨论】:

      【解决方案3】:

      android:scrollHorizo​​ntally="true" 使用这个

      【讨论】:

        猜你喜欢
        • 2014-08-17
        • 1970-01-01
        • 1970-01-01
        • 2012-10-29
        • 1970-01-01
        • 2014-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多