【问题标题】:android:How to make triangular layoutandroid:如何制作三角形布局
【发布时间】:2017-05-02 12:15:17
【问题描述】:

我想为谷歌地图制作自定义信息窗口 我可以做到,但我无法制作三角形波纹管布局。 我可以在那里添加图像,但布局在外线上有阴影。 任何人都建议我该怎么做。 如何在红色区域内制作部分。如您所见,外部布局有阴影。

【问题讨论】:

标签: android android-layout android-shapedrawable


【解决方案1】:

您可以使用材质组件库创建自定义shapes
该库提供了TriangleEdgeTreatment,它创建了一个给定大小的三角形处理,相对于形状朝内或朝外。

你可以使用类似的东西:

TriangleEdgeTreatment triangleEdgeTreatment = new TriangleEdgeTreatment(radius,false);

LinearLayout linearLayout= findViewById(R.id.xxxx);
ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
    .toBuilder()
    .setBottomEdge(triangleEdgeTreatment)
    .build();

MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
ViewCompat.setBackground(linearLayout,shapeDrawable);

此外,对于边缘处理,父视图必须通过以下方式禁用子视图的剪辑 在 xml.clipToPadding 中设置 android:clipChildren="false" 可能还需要为 false 如果父级上有任何可能与阴影相交的填充。

【讨论】:

    【解决方案2】:

    您可以使用<layer-list> 创建此自定义形状。下面是一个工作示例。将custom_triangular_shape.xml 放入您的res/drawable 文件夹。

    custom_triangular_shape.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
        <!-- Transparent Rectangle -->
        <item>
            <shape android:shape="rectangle">
                <size
                    android:width="300dp"
                    android:height="80dp" />
                <solid android:color="@android:color/transparent" />
            </shape>
        </item>
    
        <!-- Colored Rectangle -->
        <item
            android:bottom="20dp">
            <shape android:shape="rectangle">
                <corners
                    android:radius="4dp">
    
                </corners>
                <size
                    android:width="300dp"
                    android:height="80dp" />
                <solid android:color="#FFFFFF" />
            </shape>
        </item>
    
        <!-- Bottom Triangle -->
        <item
            android:left="90dp"
            android:right="110dp"
            android:top="0dp"
            android:bottom="30dp">
            <rotate android:fromDegrees="45">
                <shape android:shape="rectangle">
                    <solid android:color="#FFFFFF" />
                </shape>
            </rotate>
        </item>
    
    </layer-list>
    

    使用:

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/custom_triangular_shape">
    
    </LinearLayout>
    

    输出:

    希望对你有帮助~

    【讨论】:

    • this anwer 非常接近我的问题,但在我提到它的缺失部分也有阴影效果。如果我给它阴影,那么内部矩形也有我不想要的阴影。我只想展示外线。请建议我。如果你有什么。
    • 对于阴影,您必须添加另外 2 个带有顶部填充的矩形。
    • 能否请您更改您的代码。因为我对 android 很陌生。
    • 已经考虑过了。顺便说一句,我自己实现了,但如果你有什么新的,请更新
    • Google 的窗口将根据内部内容调整大小。在这里,您将大小硬编码到图层列表中。不完全一样。
    【解决方案3】:

    这可以使用MaterialCardViewShape theming 来实现。

    依赖关系:(使用latest stable version

    def materialVersion = '1.3.0'
    
    implementation "com.google.android.material:material:$materialVersion"
    

    布局:

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <com.google.android.material.card.MaterialCardView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:cardBackgroundColor="@color/backgroundWhite"
            app:cardElevation="4dp"
            app:cardCornerRadius="8dp">
    
            //Content layout here          
    
        </com.google.android.material.card.MaterialCardView>
    
        <com.google.android.material.card.MaterialCardView
            style="@style/PointerCardViewStyle"
            android:layout_width="20dp"
            android:layout_height="10dp"
            app:cardBackgroundColor="@color/whiteColor"
            android:layout_gravity="center"
            app:cardElevation="4dp" />
    </LinearLayout>
    

    风格:

    <style name="PointerCardViewStyle" parent="Widget.MaterialComponents.CardView">
        <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlayPointerCardView</item>
    </style>
    
    <style name="ShapeAppearanceOverlayPointerCardView" parent="@style/Widget.MaterialComponents.CardView">
        <item name="cornerFamily">cut</item>
        <item name="cornerSizeTopRight">0dp</item>
        <item name="cornerSizeTopLeft">0dp</item>
        <item name="cornerSizeBottomRight">10dp</item>
        <item name="cornerSizeBottomLeft">10dp</item>
    </style>
    

    结果:

    【讨论】:

      【解决方案4】:

      试试这个:

      对于三角形:

          <?xml version="1.0" encoding="utf-8"?>
      <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
          <item>
              <rotate
                  android:fromDegrees="45"
                  android:pivotX="-40%"
                  android:pivotY="87%"
                  android:toDegrees="45">
                  <shape android:shape="rectangle">
                      <stroke
                          android:width="10dp"
                          android:color="@android:color/transparent" />
                      <solid android:color="#000000" />
                  </shape>
              </rotate>
          </item>
      </layer-list>
      

      对于矩形:

          <?xml version="1.0" encoding="utf-8"?>
      <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
          <item>
              <shape android:shape="rectangle">
                  <solid android:color="#B2E3FA" />
              </shape>
          </item>
      </layer-list>
      

      在布局中同时使用 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">
      
          <RelativeLayout
              android:id="@+id/rlv1"
              android:layout_width="150dp"
              android:layout_height="50dp"
              android:background="@drawable/rectringle" />
          <RelativeLayout
              android:id="@+id/rlv2"
              android:layout_width="50dp"
              android:layout_height="50dp"
              android:layout_below="@+id/rlv1"
              android:layout_marginLeft="30dp"
              android:background="@drawable/tringle"
              android:rotation="180" />
      </LinearLayout>
      

      【讨论】:

      • 我赞成你的努力,但你忽略了很多东西,比如外线的阴影。这种情况下你的方法就行不通了。
      【解决方案5】:

      您可以使用图层列表。在底部创建一个三角形,然后创建一个矩形作为下一层,并提供与三角形高度相匹配的底部边距。

      这是一个制作三角形的示例。 https://stackoverflow.com/a/18421795/1987045

      【讨论】:

        【解决方案6】:

        您可以使用9patch 作为自定义布局的背景。

        【讨论】:

          【解决方案7】:

          使用PopUpWindow 可以实现您的目标。 Here 是一个很好的起点。

          【讨论】:

          • 我不想知道如何实现自定义信息窗口。我只想知道如何使布局看起来像给定的图像。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-01-04
          • 1970-01-01
          • 1970-01-01
          • 2011-03-30
          • 1970-01-01
          • 2021-07-11
          • 1970-01-01
          相关资源
          最近更新 更多