Programing

Android LinearLayout 주위에 테두리를 만들려면 어떻게해야합니까?

lottogame 2020. 8. 24. 20:50
반응형

Android LinearLayout 주위에 테두리를 만들려면 어떻게해야합니까?


큰 레이아웃 하나와 그 안에 작은 레이아웃 하나가 있습니다.

작은 레이아웃 주위에 선 테두리를 만들려면 어떻게합니까?


확실한. 원하는 레이아웃에 테두리를 추가 할 수 있습니다. 기본적으로 사용자 지정 드로어 블을 만들고 레이아웃에 배경으로 추가해야합니다. 예:

customborder.xml드로어 블 폴더에 라는 파일을 만듭니다 .

<?xml version="1.0" encoding="UTF-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
   <corners android:radius="20dp"/> 
   <padding android:left="10dp" android:right="10dp" android:top="10dp" android:bottom="10dp"/>
   <stroke android:width="1dp" android:color="#CCCCCC"/>
 </shape>

이제 작은 레이아웃에 배경으로 적용하십시오.

<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/customborder">

그게 트릭을해야합니다.

참조 :


아래와 같이 drawable 폴더에 border.xml이라는 XML을 만듭니다.

<?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="#FF0000" /> 
    </shape>
  </item>   
    <item android:left="5dp" android:right="5dp"  android:top="5dp" >  
     <shape android:shape="rectangle"> 
      <solid android:color="#000000" />
    </shape>
   </item>    
 </layer-list>

그런 다음 이것을 선형 레이아웃에 다음과 같이 backgound로 추가하십시오.

     android:background="@drawable/border"

드로어 블 폴더에 하나의 xml 파일 만들기

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

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

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

이제이 xml을 작은 레이아웃 배경으로 호출하십시오.

android : background = "@ drawable / yourxml"


이 시도:

For example, let's define res/drawable/my_custom_background.xml as:

(create this layout in your drawable folder) layout_border.xml

  <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <stroke android:width="2dp" android:height="2dp"
                android:color="#FF0000" />
            <solid android:color="#000000" />
            <padding android:left="1dp" android:top="1dp" android:right="1dp"
                android:bottom="1dp" />

            <corners android:radius="1dp" android:bottomRightRadius="5dp"
                android:bottomLeftRadius="0dp" android:topLeftRadius="5dp"
                android:topRightRadius="0dp" />
        </shape>
    </item>

</layer-list>

main.xml

<LinearLayout 
    android:layout_gravity="center"
    android:layout_width="200dp" 
    android:layout_height="200dp"   
    android:background="@drawable/layout_border" />
</LinearLayout>

This solution will only add the border, the body of the LinearLayout will be transparent.

First, Create this border drawable in the drawable folder, border.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android= "http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke android:width="2dp" android:color="#ec0606"/>
    <corners android:radius="10dp"/>
</shape>

Then, in your LinearLayout View, add the border.xml as the background like this

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/border">

you can do it Pragmatically also

  GradientDrawable gradientDrawable=new GradientDrawable();
   gradientDrawable.setStroke(4,getResources().getColor(R.color.line_Input));

Then set the background of layout as :

LinearLayout layout = (LinearLayout ) findViewById(R.id.ayout); layout .setBackground(gradientDrawable);

I'll add Android docs link to other answers.

https://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

It describes all attributes of the Shape Drawable and stroke among them to set the border.

Example:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
  <stroke android:width="1dp" android:color="#F00"/>
  <solid android:color="#0000"/>
</shape>

Red border with transparent background.


Don't want to create a drawable resource?

  <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/black"
    android:minHeight="128dp">

    <FrameLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_margin="1dp"
      android:background="@android:color/white">

      <TextView ... />
    </FrameLayout>
  </FrameLayout>

It's not exactly an answer for the question but for them wich just want to display the border of elements for debugging you could just go in your devloppers settings and turn on the show layout bounds in the drawing section

참고URL : https://stackoverflow.com/questions/15111402/how-can-i-create-a-border-around-an-android-linearlayout

반응형