Cách áp dụng bán kính góc cho LinearLayout


Câu trả lời:


278

Bạn có thể tạo tệp XML trong thư mục có thể vẽ. Gọi nó chẳng hạnshape.xml

Trong shape.xml:

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"   >

    <solid
        android:color="#888888" >
    </solid>

    <stroke
        android:width="2dp"
        android:color="#C4CDE0" >
    </stroke>

    <padding
        android:left="5dp"
        android:top="5dp"
        android:right="5dp"
        android:bottom="5dp"    >
    </padding>

    <corners
        android:radius="11dp"   >
    </corners>

</shape>

Các <corner>thẻ là cho câu hỏi cụ thể của bạn.

Thực hiện các thay đổi theo yêu cầu.

Và trong whatever_layout_name.xml:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_margin="5dp"
    android:background="@drawable/shape"    >
</LinearLayout>

Đây là những gì tôi thường làm trong ứng dụng của mình. Hi vọng điêu nay co ich....


Cách đặt hình nền trong xml này
vignesh

1
@vignesh: Cái nào có thể vẽ được và đặt nó ở đâu? Nếu bạn có nghĩa là <shape>ví dụ, nó đã được đặt trong XML bố cục ở đây:android:background="@drawable/shape"
Siddharth Lele

3
Điều gì sẽ xảy ra nếu bố cục tuyến tính này đã có hình nền và tôi muốn nó có bán kính góc? trong mã của bạn i wont có thể thiết lập một hình nền, kể từ khi sở hữu nền LinearLayout được thiết lập với các shape.xml
newton_guima

@MrAppleBR: Tôi sẽ không thể đặt hình nền : Đúng. Nhưng trong bối cảnh câu hỏi, OP đã có một trường hợp sử dụng mà điều này là hợp lệ. Trong trường hợp sử dụng mà bạn đề cập, đây không phải là điều bạn nên làm.
Siddharth Lele

@SiddharthLele Tôi nên làm gì? bạn có thể giải thích với một nguồn nhỏ hoặc có thể là một liên kết? cảm ơn!
newton_guima


8

Bố trí

<LinearLayout 
    android:id="@+id/linearLayout"
    android:layout_width="300dp"
    android:gravity="center"
    android:layout_height="300dp"
    android:layout_centerInParent="true"
    android:background="@drawable/rounded_edge">
 </LinearLayout>

Thư mục có thể vẽ được round_edge.xml

<shape 
xmlns:android="http://schemas.android.com/apk/res/android">
    <solid 
        android:color="@android:color/darker_gray">
    </solid>
    <stroke 
         android:width="0dp" 
         android:color="#424242">
    </stroke>
    <corners 
         android:topLeftRadius="100dip"
         android:topRightRadius="100dip"
         android:bottomLeftRadius="100dip"
         android:bottomRightRadius="100dip">
    </corners>
</shape>

2

hãy thử điều này, đối với Lập trình để đặt nền có bán kính thành LinearLayout hoặc bất kỳ Chế độ xem nào.

 private Drawable getDrawableWithRadius() {

    GradientDrawable gradientDrawable   =   new GradientDrawable();
    gradientDrawable.setCornerRadii(new float[]{20, 20, 20, 20, 20, 20, 20, 20});
    gradientDrawable.setColor(Color.RED);
    return gradientDrawable;
}

LinearLayout layout = new LinearLayout(this);
layout.setBackground(getDrawableWithRadius());
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.