将android EditText样式从矩形边框更改为下划线

gwbalxhn  于 2022-12-02  发布在  Android
关注(0)|答案(5)|浏览(151)

在我的一个活动中,我的EditText视图通常如下所示

但现在他们看起来像这样

我需要帮助将其更改回来:从矩形到下划线。

背景

因为我需要创建一个自定义ActionBar,所以我必须使用以下代码更改所讨论活动的主题YesterdayActivity
款式:

<style name="CustomWindowTitleBackground">
        <item name="android:background">#323331</item>
    </style>

    <style name="CustomTheme" parent="android:Theme">
        <item name="android:windowTitleSize">40dip</item>
        <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
    </style>

清单:

<activity
        android:name="com.example.YesterdayActivity"
        android:theme="@style/CustomTheme">
    </activity>

创建时:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_yesterday);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.yesterday_title_bar);
…
}
nwsw7zdq

nwsw7zdq1#

按如下方式更改自定义主题

<style name="CustomTheme" parent="android:Theme.Holo.NoActionBar">

因为你没有使用旧的Android主题,而不是HoLo,它有那种editTextView
在较新版本的Android中,只要选择了Holo主题,框架就会使用Window.FEATURE_ACTION_BAR功能。只要应用调用setFeatureInt(Window.FEATURE_CUSTOM_TITLE)并且已经设置了FEATURE_ACTION_BAR,框架就会抛出异常。它崩溃是因为Holo默认使用ActionBar。修复很简单。使用Holo时关闭ActionBar

rjee0c15

rjee0c152#

从样式中删除此线条

<item name="android:background">#323331</item>

因为它是反映行为的背景属性。

hmmo2u0o

hmmo2u0o3#

有两种方法来实现这一点。第一种方法是通过改变你的应用程序的主题
第一种方法:

安卓清单.xml

  • 请参阅android:主题 *
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    ...
    ...
    </application>

资源/值/样式.xml

  • 主题.应用程序兼容.浅色.深色ActionBar * 使编辑文本带有下划线
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

只需以相同方式使用EditText

<EditText
      android:layout_width="match_parent"
      android:layout_height="48dp"
      android:hint="Email Address"
      android:inputType="textMultiLine"
      android:textSize="16dp" />

第二种方法:
创建一个带有底部笔划的可绘制形状,并将其设置为EditText的背景
创建一个名为res/drawable/stroke_edittext_bg.xml的可绘制文件如以下链接所示:
How To Create An Android Shape With Bottom Stroke

<EditText
      android:layout_width="match_parent"
      android:layout_height="48dp"
      android:background="@drawable/stroked_edittext_bg"
      android:hint="Email Address"
      android:inputType="textMultiLine"
      android:textSize="16dp" />
svdrlsy4

svdrlsy44#

如果您更改editText的主题,则可以实现。

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Name"
        android:theme="@android:style/Theme.Holo"
        android:ems="10"
        android:id="@+id/editText"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true" />
xzabzqsa

xzabzqsa5#

<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>

将其更改回默认样式。或者从代码中删除此行。
因为您共享的第一个图像是默认的Editext背景。

相关问题