【问题标题】:Adding Shape to LinearLayout Android将形状添加到 LinearLayout Android
【发布时间】:2013-04-15 11:52:10
【问题描述】:

我有一个带有一些自动完成和文本框的线性布局。我想在线性布局中插入一个形状(矩形)。我怎样才能做到这一点。我是安卓新手。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"    
    android:padding="5dp"
    android:id="@+id/layout">

    <AutoCompleteTextView android:id="@+id/autocompleteCountry"
      android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/CONTRY_LABEL"
       />
    <AutoCompleteTextView android:id="@+id/locationAutoCompleteFrom"
      android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/from"
        android:visibility="gone"
       />
   <AutoCompleteTextView android:id="@+id/locationAutoCompleteTO"
      android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/to"
        android:visibility="gone"
       />
 <!--    <Button android:id="@+id/buttonRoute"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/buttonRouteText"
       android:enabled="false"
       android:clickable="true"
       />
   -->

【问题讨论】:

    标签: android xml android-layout layout shape


    【解决方案1】:

    您应该使用 ShapeDrawable 作为要添加样式的组件的背景。

    使用以下语法在 XML 中创建一个可绘制的形状(这个显示了一个带有圆角的矩形,但是您可以做很多事情来自定义它以使其看起来像您想要的那样)。这个文件(命名为 background_square.xml 或其他)应该放在你的 drawable 文件夹中:

     <?xml version="1.0" encoding="utf-8"?>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <corners android:radius="5dp" />
        <solid
            android:color="@color/primary_grey" /> 
    </shape>
    

    然后,当您想将其添加到视图时,您可以在 XML 中使用以下语法:

    android:background="@drawable/background_square"
    

    【讨论】:

    • 我使用这个形状作为 ImageView 的来源,但其他都很好!谢谢!
    • 您可以将此背景添加到任何视图 - ImageView、TextView、LinearLayout(在这种情况下,就 Android 而言,所有视图都相同)。
    【解决方案2】:

    您可以执行以下操作

    如果你想添加矩形,只需在你的布局中添加嵌套布局(比如线性布局) 并设置android:background="yourcolor" //您可以使用哈希颜色值添加颜色

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"    
    android:padding="5dp"
    android:id="@+id/layout">
    
    <AutoCompleteTextView android:id="@+id/autocompleteCountry"
      android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/CONTRY_LABEL"
       />
    <AutoCompleteTextView android:id="@+id/locationAutoCompleteFrom"
      android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/from"
        android:visibility="gone"
       />
    //suppose you want to add your rectangle here
    <LinearLayout 
    android:id="@+id/rectangle"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"    
    android:background="yourcolor"
    >
    </LinearLayout>
    

    您可以根据需要更改所有相关属性,例如大小、边距等

    【讨论】:

    • 实际上你在正确的道路上,但如果你只是添加一个矩形,那么使用 LinearLayout 是没有意义的。改用 View,它具有用于设置 LineraLayout 样式的所有主要属性,并且在布局过程中的计算方面可能更轻
    【解决方案3】:

    我可能会建议通过实现 Linearlayout 类来创建自定义视图并覆盖 ondraw() mtd 以绘制你想要的任何形状。希望这可能有助于朝着正确的方向前进

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 2011-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      相关资源
      最近更新 更多