我希望EditText的默认余量为10dp.因此,在我的styles.xml文件中,我设置了以下内容:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyTheme" parent="android:style/Theme.NoTitleBar">
<item name="android:editTextStyle">@style/edit_text_default</item>
</style>
<style name="edit_text_default" parent="android:style/Widget.EditText">
<item name="android:layout_margin">10dp</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
然后在AndroidManifest.xml中,我将应用程序主题设置为我定义的主题:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MyTheme" >
...
Run Code Online (Sandbox Code Playgroud)
该主题的"无标题栏"方面正在发挥作用.但是,EditText的默认边距不是,它仍然填充父级.这是我的表格视图:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >
<TableRow>
<EditText android:hint="@string/last_name" />
</TableRow>
<TableRow>
<EditText android:hint="@string/first_name" />
</TableRow>
</TableLayout>
Run Code Online (Sandbox Code Playgroud) 默认的EditText背景似乎在左边放了~4dp的填充.这会导致与其他小部件不对齐.
我做了一个简单的应用程序来演示.截图:
布局:
<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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:background="#00ff00"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="HINT"
android:text="TEXT"
android:singleLine="true"/>
<TextView
android:background="#00ff00"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
反正有没有阻止这样做?
我面临着问题.我试图让我的应用程序中的编辑文本在主题的帮助下看起来一样.
我已经在在线工具(9-patch图像)的帮助下生成了样式,并尝试在我的主题中设置它
<!-- Base application theme. -->
<style name="DefaultAppTheme" parent="Theme.AppCompat">
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="colorPrimary">@color/blue</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">@color/darkblue</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">@color/cornflowerblue</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:editTextStyle">@style/EditTextDefault</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我的编辑文本样式
<style name="EditTextDefault" parent="android:Widget.EditText">
<item name="android:background">@drawable/edit_text_holo_light</item>
<item name="android:textColor">@color/light_blue</item>
<item name="android:layout_marginLeft">@dimen/margin_edit_text_default</item>
<item name="android:layout_marginRight">@dimen/margin_edit_text_default</item>
<item name="android:layout_marginTop">@dimen/margin_edit_text_default</item>
</style>
Run Code Online (Sandbox Code Playgroud)
但是,如果使用编辑文本而不为每个编辑文本手动指定样式,则此操作无效.
这不起作用
<EditText
android:id="@+id/edit_text_login"
android:layout_width="match_parent" …Run Code Online (Sandbox Code Playgroud) 我在 AndroidStudio 中在 EditText 下划线时遇到了一些问题。
这就是我要找的(这只是一张照片,不是我的真实文字):

但我真的不知道有什么财产可以做到这一点。
我现在的代码非常简单。只是“正常”的一个:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_margin="16dp"
android:id="@+id/tx_titulo"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="@string/pt_titulo"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:allowUndo="true"
/>
Run Code Online (Sandbox Code Playgroud)
我不想只在文本下划线(在本例中为“Título”),而是整个 PlainText
有人知道吗?问候。