在我的一个应用程序中,我需要为线性布局创建一个边框,如下所示
我不想有图像并将其设置为背景。因为那时我需要为不同的设备创建各种尺寸的图像。
如果我使用线性布局创建布局并使用绝对定位放置文本视图,则它在不同设备中可能看起来不符合预期。
那么实现这个 UI 的最佳方法是什么?
Try this
main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="10dp"
android:background="@drawable/Layout_selector"
android:orientation="vertical" >
</LinearLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MyText"
android:layout_marginLeft="50dp"
android:padding="1dp"
android:background="#fff"
android:textAppearance="?android:attr/textAppearanceMedium" />
</FrameLayout>
</LinearLayout>
在可绘制中布局选择器.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true"
android:state_pressed="true">
<shape android:padding="5dp" android:shape="rectangle">
<solid android:color="#FFF" />
<stroke android:width="0.5dp" android:color="#29166f" />
<corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" />
</shape>
</item>
<item android:state_enabled="true" android:state_focused="true">
<shape android:padding="5dp" android:shape="rectangle">
<solid android:color="#FFF" />
<stroke android:width="0.5dp" android:color="#29166f" />
<corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" />
</shape>
</item>
<item android:state_enabled="true">
<shape android:padding="5dp"
android:shape="rectangle">
<solid android:color="#FFF" />
<stroke android:width="0.5dp" android:color="#29166f" />
<corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" />
</shape>
</item>
</selector>
输出是:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)