【问题标题】:Text in EditText is not at Center with center tagEditText 中的文本不在带有中心标签的中心
【发布时间】:2016-10-07 18:02:14
【问题描述】:

大家好。

我为我的应用创建了一个页面,但输入的文本不在垂直中心。 XML 后面有屏幕截图。我玩过xml部分,没有得到任何结果。

这是 XML 部分:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f3f2f2"
    android:orientation="vertical"
    android:weightSum="10.0" >
    <!-- Header -->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.5" >

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clickable="false"
                android:scaleType="fitXY"
                android:src="@drawable/header_half" />
        </FrameLayout>
    </FrameLayout>

    <TextView
        android:id="@+id/header_title_free_vib_xml"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:gravity="center"
        android:text="@string/free_vibration_one_line_strg"
        android:textColor="#ed1c24"
        android:textSize="24dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="6.0"
        android:orientation="horizontal" >

        <View
            android:layout_width="20dp"
            android:layout_height="match_parent"
            android:background="#838383" />

        <LinearLayout
            android:id="@+id/big_body_center_edittext"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="10.0" >

            <!-- Mass -->

            <LinearLayout
                android:id="@+id/ll_mass_xml"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginLeft="15dp"
                android:layout_weight="2.0"
                android:orientation="horizontal"
                android:weightSum="100.0" >

                <TextView
                    android:id="@+id/tv_mass_xml"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="20.0"
                    android:gravity="left|center"
                    android:text="@string/mass_strg"
                    android:textColor="#838383"
                    android:textSize="16dp" />

                <EditText
                    android:id="@+id/et_mass_xml"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="70.0"
                    android:background="#e7e7e7"
                    android:cursorVisible="false"
                    android:gravity="center"
                    android:hint="@string/mass_boundary_for_editext_strg"
                    android:inputType="numberDecimal"
                    android:textColor="#bcbcbc" />

                <TextView
                    android:id="@+id/tv_mass_unit_xml"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="10.0"
                    android:background="#adadad"
                    android:gravity="center"
                    android:text="@string/cm_strg"
                    android:textColor="#f3f2f2"
                    android:textSize="10dp" />
            </LinearLayout>

            <!-- Stiffness -->

            <LinearLayout
                android:id="@+id/ll_stiffness_xml"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="20dp"
                android:layout_weight="2.0"
                android:orientation="horizontal"
                android:weightSum="100.0" >

                <TextView
                    android:id="@+id/tv_stiffness_xml"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="20.0"
                    android:gravity="left|center"
                    android:text="@string/stiffness_strg"
                    android:textColor="#838383"
                    android:textSize="16dp" />

                <EditText
                    android:id="@+id/et_stiffness_xml"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="70.0"
                    android:background="#e7e7e7"
                    android:cursorVisible="false"
                    android:gravity="center"
                    android:hint="@string/mass_boundary_for_editext_strg"
                    android:inputType="numberDecimal"
                    android:textColor="#bcbcbc" />

                <TextView
                    android:id="@+id/tv_stiffness_unit_xml"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="10.0"
                    android:background="#adadad"
                    android:gravity="center"
                    android:text="@string/cm_strg"
                    android:textColor="#f3f2f2"
                    android:textSize="10dp" />
            </LinearLayout>

            <!-- Damping -->

            <LinearLayout
                android:id="@+id/ll_damping_xml"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="20dp"
                android:layout_weight="2.0"
                android:orientation="horizontal"
                android:weightSum="100.0" >

                <TextView
                    android:id="@+id/tv_damping_xml"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="20.0"
                    android:gravity="left|center"
                    android:text="@string/damping_strg"
                    android:textColor="#838383"
                    android:textSize="16dp" />

                <EditText
                    android:id="@+id/et_damping_xml"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="70.0"
                    android:background="#e7e7e7"
                    android:cursorVisible="false"
                    android:gravity="center"
                    android:hint="@string/damping_boundary_for_editext_strg"
                    android:inputType="numberDecimal"
                    android:textColor="#bcbcbc" />

                <TextView
                    android:id="@+id/tv_damping_unit_xml"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="10.0"
                    android:background="#adadad"
                    android:gravity="center"
                    android:text="@string/cm_strg"
                    android:textColor="#f3f2f2"
                    android:textSize="10dp" />
            </LinearLayout>

            <!-- Boundary Conditions -->

            <LinearLayout
                android:id="@+id/ll_boundary_conditions_xml"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="20dp"
                android:layout_weight="4.0"
                android:orientation="horizontal"
                android:weightSum="100.0" >

                <TextView
                    android:id="@+id/tv_boundary_conditions_xml"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="20.0"
                    android:gravity="left|center"
                    android:text="@string/boundary_conditions_strg"
                    android:textColor="#838383"
                    android:textSize="12dp" />

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="70.0"
                    android:orientation="vertical"
                    android:weightSum="2.0" >

                    <EditText
                        android:id="@+id/et_bc_initial_velocity_xml"
                        android:layout_width="fill_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1.0"
                        android:background="#e7e7e7"
                        android:cursorVisible="false"
                        android:gravity="center"
                        android:hint="@string/bc_inivelo_boundary_for_et_strg"
                        android:inputType="numberDecimal"
                        android:textColor="#bcbcbc" />

                    <EditText
                        android:id="@+id/et_bc_initial_displacement_xml"
                        android:layout_width="fill_parent"
                        android:layout_height="0dp"
                        android:layout_marginTop="5dp"
                        android:layout_weight="1.0"
                        android:background="#e7e7e7"
                        android:cursorVisible="false"
                        android:gravity="center"
                        android:hint="@string/bc_inidis_boundary_for_et_strg"
                        android:inputType="numberDecimal"
                        android:textColor="#bcbcbc" />
                </LinearLayout>

                <TextView
                    android:id="@+id/tv_boundary_conditions_unit_xml"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="10.0"
                    android:background="#adadad"
                    android:gravity="center"
                    android:text="@string/cm_strg"
                    android:textColor="#f3f2f2"
                    android:textSize="10dp" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
    <!-- END OF ALL EditTexts -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2.0"
        android:gravity="center" >

        <ImageButton
            android:id="@+id/free_vib_proceed_xml"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_gravity="center_horizontal|center"
            android:background="@xml/round_circle_btn"
            android:scaleType="fitXY"
            android:src="@drawable/proceed" />
    </LinearLayout>

</LinearLayout>

我也为 EditText 使用了填充,但不幸的是没有用。

EditText.setPadding(0, 0, 0, 0);

我使用 TextWatcher 来改变提示的大小:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.four_main_free_vib);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

if((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE){
            TextSize_Value_NormalText = 22;
            TextSize_Value_BiggerText = 84;

            ResizeHintter(et_INI_V, 84, 30);
            ResizeHintter(et_INI_D, 84, 30);
            ResizeHintter(et_D, 84, 48);
            ResizeHintter(et_S, 84, 48);
            ResizeHintter(et_M, 84, 48);

        } else if((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
            TextSize_Value_NormalText = 16;
            TextSize_Value_BiggerText = 54;

        } else if((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
            TextSize_Value_NormalText = 12;
            TextSize_Value_BiggerText = 34;

            ResizeHintter(et_INI_V, 42, 14);
            ResizeHintter(et_INI_DT, 42, 14);
            ResizeHintter(et_D, 42, 14);
            ResizeHintter(et_S, 42, 14);
            ResizeHintter(et_M, 42, 14);



        } else if((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
            TextSize_Value_NormalText = 8;
            TextSize_Value_BiggerText = 22;


        } else {
            TextSize_Value_NormalText = 12;
            TextSize_Value_BiggerText = 36;
        }

    }// onCreate(Bundle).

    void ResizeHintter(final EditText et, final int OriginalSize, final int HintSize){
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub
                et.setTextSize(TypedValue.COMPLEX_UNIT_DIP, HintSize);
            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                if(s.length() == 0){
                    // No entered text so will show hint
                    et.setTextSize(TypedValue.COMPLEX_UNIT_DIP, HintSize);
                    et.setPadding(0, 0, 0, 0);
                } else {
                    et.setTextSize(TypedValue.COMPLEX_UNIT_DIP, OriginalSize);
                    et.setPadding(0, 0, 0, 0);
                }
            }
        });
    }

TextWatcher 还有其他问题。规则(如果是大的话)不适用于首次活动运行。它在删除所有文本并重新输入文本后起作用。让我们看一下屏幕截图(其中 2 个屏幕截图来自 tablet-7”,但之前的屏幕截图来自手持手机 Nexus 5。

什么都没碰:

输入一些数字:

删除输入的数字,提示的大小和代码一样好(大 if-else):

所有提示必须具有更大的尺寸,代码中提到的尺寸(大 if-else)但在第一次运行时它仍然很小,并且在一些删除和输入后接受的尺寸。 我希望提示文本大小与提供的代码中的一样。

谢谢。

【问题讨论】:

  • 看看android:layout_gravity="center"是否对你有帮助..
  • @Lal 请至少阅读我的代码。我用过,不幸的是没有用。实际上在平板电脑(大尺寸设备)中它居中,但在 Nexus 5 中不是。

标签: android xml text android-edittext centering


【解决方案1】:

使用这个:

android:gravity="center"     
android:layout_width="match_parent"
android:layout_height="wrap_content"    
android:ellipsize="start" 

这适用于我的编辑文本

【讨论】:

    【解决方案2】:

    改变

    <EditText
        android:layout_height="wrap_content"
    />
    

    <EditText
        android:layout_height="match_parent"
    />
    

    【讨论】:

    • 不起作用。一些编辑文本高度是重量基础,这也不起作用。
    • 如您所见,在平板电脑中,文本明显位于中心!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 2014-08-04
    • 1970-01-01
    • 2019-07-18
    • 2021-10-30
    • 2012-09-15
    相关资源
    最近更新 更多