오른쪽에 아래쪽 삼각형이있는 스피너 주위의 테두리와 같은 사용자 지정 스피너를 만드는 방법은 무엇입니까?
오른쪽 하단 모서리에 삼각형이있는 스피너 주위의 선과 같은 커스텀 스피너를 개발하고 싶습니다.
다음 이미지처럼
위의 무화과에 대해 나는
spinner.xml
<Spinner android:background="@drawable/spinner_background"/>
spinner_background.xml
<?xml version="1.0" encoding="UTF-8"?>
<item android:state_pressed="true"
android:drawable="@drawable/spinner_ab_pressed_new_theme_bs">
<shape>
<solid
android:color="@color/White" />
<corners android:radius="3dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
<stroke
android:width="2dp"
android:color="@color/skyblue" />
</shape>
</item>
<!-- spinner_ab_default_new_theme_bs -> this image for corner triangle -->
<item
android:drawable="@drawable/spinner_ab_default_new_theme_bs" >
<shape>
<solid
android:color="@color/White">
</solid>
<corners android:radius="3dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
<stroke
android:width="2dp"
android:color="@color/gray"/>
</shape>
</item>
그리고 다음 이미지와 같은 출력을 얻었습니다.
나는 많은 것을 시도했지만 목표를 달성하지 못했습니다.
위의 첫 번째 이미지와 같습니다.
스피너
<Spinner
android:id="@+id/To_Units"
style="@style/spinner_style" />
style.xml
<style name="spinner_style">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">@drawable/gradient_spinner</item>
<item name="android:layout_margin">10dp</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingRight">20dp</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
<item name="android:popupBackground">#DFFFFFFF</item>
</style>
gradient_spinner.xml (드로어 블 폴더에 있음)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item><layer-list>
<item><shape>
<gradient android:angle="90" android:endColor="#B3BBCC" android:startColor="#E8EBEF" android:type="linear" />
<stroke android:width="1dp" android:color="#000000" />
<corners android:radius="4dp" />
<padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
</shape></item>
<item ><bitmap android:gravity="bottom|right" android:src="@drawable/spinner_arrow" />
</item>
</layer-list></item>
</selector>
@drawable/spinner_arrow
오른쪽 하단 이미지입니다
이것은 간단한 것입니다.
your_layout.xml
<android.support.v7.widget.AppCompatSpinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/spinner_background"
/>
드로어 블 폴더에서 spinner_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item><layer-list>
<item>
<shape>
<solid
android:color="@color/colorWhite">
</solid>
<corners android:radius="3dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
<stroke
android:width="2dp"
android:color="@color/colorDarkGrey"/>
</shape>
</item>
<item >
<bitmap android:gravity="bottom|right"
android:src="@drawable/ic_arrow_drop_down_black_24dp" />
</item>
</layer-list></item>
</selector>
시사:
XML의 스피너 선언에서 한 줄을 사용하여 다음을 수행 할 수 있습니다.
다음을 추가하십시오. style="@android:style/Widget.Holo.Light.Spinner"
This is a default generated style in android. It doesn't contain borders around it though. For that you'd better search something on google.
Hope this helps.
UPDATE: AFter a lot of digging I got something which works well for introducing border around spinner.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="8dp"
android:top="8dp">
<shape>
<solid android:color="@android:color/white" />
<corners android:radius="4dp" />
<stroke
android:width="2dp"
android:color="#9E9E9E" />
<padding
android:bottom="16dp"
android:left="8dp"
android:right="16dp"
android:top="16dp" />
</shape>
</item>
</layer-list>
Place this in the drawable folder and use it as a background for spinner. Like this:
<RelativeLayout
android:id="@+id/speaker_relative_layout"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:background="@drawable/spinner_style"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Spinner
android:id="@+id/select_speaker_spinner"
style="@style/Widget.AppCompat.DropDownItem.Spinner"
android:layout_width="match_parent"
android:layout_height="70dp"
android:entries="@array/select_speaker_spinner_array"
android:spinnerMode="dialog" />
</RelativeLayout>
There are two ways to achieve this.
1- As already proposed u can set the background of your spinner as custom 9 patch Image with all the adjustments made into it .
android:background="@drawable/btn_dropdown"
android:clickable="true"
android:dropDownVerticalOffset="-10dip"
android:dropDownHorizontalOffset="0dip"
android:gravity="center"
If you want your Spinner to show With various different background colors i would recommend making the drop down image transparent, & loading that spinner in a relative layout with your color set in.
btn _dropdown is as:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_window_focused="false" android:state_enabled="true"
android:drawable="@drawable/spinner_default" />
<item
android:state_window_focused="false" android:state_enabled="false"
android:drawable="@drawable/spinner_default" />
<item
android:state_pressed="true"
android:drawable="@drawable/spinner_pressed" />
<item
android:state_focused="true" android:state_enabled="true"
android:drawable="@drawable/spinner_pressed" />
<item
android:state_enabled="true"
android:drawable="@drawable/spinner_default" />
<item
android:state_focused="true"
android:drawable="@drawable/spinner_pressed" />
<item
android:drawable="@drawable/spinner_default" />
</selector>
where the various states of pngwould define your various States of spinner seleti
간단한 나인 패치 png 이미지를 디자인하여 스피너의 배경으로 사용할 수 있습니다. 김프를 사용하면 이미지에 테두리와 직각 삼각형을 모두 넣을 수 있습니다.
"배경"만 변경하려면 (모서리 추가, 색상 변경, ....) 원하는 배경 드로어 블을 사용하여 FrameLayout에 넣을 수 있습니다. 그렇지 않으면 스피너 화살표를 잃지 않도록 9 개의 패치 배경을 만들어야합니다. 스피너 배경이 투명합니다.
어댑터-> getDropDownView에 이것을 추가하는 것은 매우 쉽습니다.
getDropDownView :
convertView.setBackground(getContext().getResources().getDrawable(R.drawable.bg_spinner_dropdown));
bg_spinner_dropdown :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/white" />
<corners
android:bottomLeftRadius=enter code here"25dp"
android:bottomRightRadius="25dp"
android:topLeftRadius="25dp"
android:topRightRadius="25dp" />
</shape>
'Nice programing' 카테고리의 다른 글
Jersey 클라이언트를 사용하여 POST 작업 수행 (0) | 2021.01.09 |
---|---|
일반 텍스트 영역 (0) | 2021.01.09 |
git push heroku master가 "모든 최신 정보"라고 말하지만 앱이 최신 상태가 아닙니다. (0) | 2021.01.09 |
pyspark 데이터 프레임 집계에 대한 열 이름 변경 (0) | 2021.01.09 |
ES6지도 객체를 정렬 할 수 있습니까? (0) | 2021.01.09 |