带有填充平铺图像的Android圆角布局

时间:2014-10-14 18:40:04

标签: java android xml android-layout layout

我希望有一个xml解决方案...我正在尝试创建一个具有圆角的布局,然后用图像填充该布局(平铺,重复)。所以基本上我有一个LinearLayout,它有一个分配给背景的drawble分层列表。这是测试布局(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" >

    <LinearLayout
        android:id="@+id/rate_buttons"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:padding="0dp" 
        android:background="@drawable/test_rounded">
    </LinearLayout>
</LinearLayout>

这是test_rounded drawable xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item ><bitmap android:src="@drawable/my_tiled_image" android:tileMode="repeat"/></item>
    <item >
        <shape xmlns:android="http://schemas.android.com/apk/res/android" >     
            <stroke android:width="3dip" android:color="#B1BCBE" />
            <corners android:radius="20dip"/>   
        </shape>
    </item>
</layer-list>

图像溢出形状/布局边框的角落。我得到以下

corners are overflowed here

你可以看到角落溢出。我听说过修改代码中的图像部分(蓝色图案),但我遇到了问题,因为我修改它并沿途设置动画。所以,如果它可以只用这个图像填写布局,请告诉我。谢谢大家的帮助。

2 个答案:

答案 0 :(得分:1)

您只能以编程方式执行此操作。但是如果你想使用xml这样做,你必须在相对布局和设置边距的帮助下,在圆角布局上方的布局中添加背景图像。这不会提供完美的解决方案,但会解决您的问题。

第一种是XML方式 在相应的可绘制文件夹中为圆形边框创建一个xml文件。 我把它命名为popupborder.xml。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#FFFFFF" />
<stroke android:width="2dp" />
<corners android:radius="15dip" />
</shape>


The main layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="@drawable/popupborder" >

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp" >

<LinearLayout
    android:id="@+id/property_details"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Inner view with Background and margin"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#000000" />
</LinearLayout>
</RelativeLayout>

答案 1 :(得分:0)

我建议您使用图像加载程序库,例如通用图像加载程序,毕加索等。以下是一个示例代码:

String url = "file://" + your_file_path
com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(url, ivPicture, options);

你必须添加

.displayer(new RoundedBitmapDisplayer(px))

到Universal Image Loader显示选项。

还有另一个图书馆(我认为这就是你要找的)https://github.com/vinc3m1/RoundedImageView

您可以查看此内容。但从第二种方面来说,你可以得到“内存异常”#34;加载大尺寸图像的原因。

我希望这会帮助你。