Programming

Android ListView 구분선의 색상을 변경하는 방법은 무엇입니까?

procodes 2020. 2. 22. 12:02
반응형

Android ListView 구분선의 색상을 변경하는 방법은 무엇입니까?


ListView구분선의 색을 변경하고 싶습니다 . 도움을 주시면 감사하겠습니다.


을 사용하여 레이아웃 xml 파일에서이 값을 설정할 수 있습니다 android:divider="#FF0000". 색상 / 드로어 블을 변경하는 경우 디바이더의 높이도 설정 / 재설정해야합니다.

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <ListView 
    android:id="@+id/android:list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:divider="#FFCC00"
    android:dividerHeight="4px"/>

</LinearLayout>

또는 코드를 작성할 수 있습니다.

int[] colors = {0, 0xFFFF0000, 0}; // red for the example
myList.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
myList.setDividerHeight(1);

그것이 도움이되기를 바랍니다.


단일 컬러 라인 사용 :

list.setDivider(new ColorDrawable(0x99F10529));   //0xAARRGGBB
list.setDividerHeight(1);

DividerHeight는 분배기 뒤에 설정하는 것이 중요합니다 . 그렇지 않으면 아무것도 얻지 못합니다.


다음을 사용하여 리소스에서 색상을 얻을 수도 있습니다.

dateView.setDivider(new ColorDrawable(_context.getResources().getColor(R.color.textlight)));
dateView.setDividerHeight(1);

@Asher Aslan 멋진 효과를위한 XML 버전.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient
        android:angle="180"
        android:startColor="#00000000"
        android:centerColor="#FFFF0000"
        android:endColor="#00000000"/>

</shape>

해당 모양의 이름은 drawable 폴더 아래의 list_driver.xml입니다.

<ListView
        android:id="@+id/category_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:divider="@drawable/list_driver"
        android:dividerHeight="5sp" />

동일한 작업을 수행하는 두 가지 방법이 있습니다.

  1. 레이아웃 xml 파일에서 android : divider = "# FFCCFF" 값을 설정할 수 있습니다 . 이를 통해 android : dividerHeight = "5px " 와 같이 디바이더의 높이를 지정해야합니다 .

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
      <ListView 
      android:id="@+id/lvMyList"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:divider="#FFCCFF"
      android:dividerHeight="5px"/>
    
    </LinearLayout>
    
  2. 프로그래밍 방식 으로이 작업을 수행 할 수도 있습니다 ...

    ListView listView = getListView();
    ColorDrawable myColor = new ColorDrawable(
        this.getResources().getColor(R.color.myColor)
    );
    listView.setDivider(myColor);
    listView.setDividerHeight();
    

xml 파일에서 아래 코드를 사용하십시오.

<ListView 
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="#000000" 
    android:dividerHeight="1dp">
</ListView> 

프로그래밍 방식으로 사용

           // Set ListView divider color
            lv.setDivider(new ColorDrawable(Color.parseColor("#FF4A4D93")));

            // set ListView divider height
            lv.setDividerHeight(2);

xml 사용

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <ListView 
    android:id="@+id/android:list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:divider="#44CC00"
    android:dividerHeight="4px"/>

</LinearLayout>

android:divider="#FF0000"android:dividerHeight="2px"ListView에 사용하십시오 .

<ListView 
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#0099FF"
android:dividerHeight="2px"/>

참고 URL : https://stackoverflow.com/questions/2372415/how-to-change-color-of-android-listview-separator-line



반응형