레이아웃을 부풀려서 사용자 정의 보기를 작성하시겠습니까?
여러 곳에서 사용하는 특정 레이아웃을 대체할 사용자 정의 뷰를 작성하려고 하는데, 그렇게 하기가 힘들어요.
기본적으로, 저는 이것을 대체하고 싶습니다.
<RelativeLayout
android:id="@+id/dolphinLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/background_box_light_blue"
android:padding="10dip"
android:layout_margin="10dip">
<TextView
android:id="@+id/dolphinTitle"
android:layout_width="200dip"
android:layout_height="100dip"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dip"
android:text="@string/my_title"
android:textSize="30dip"
android:textStyle="bold"
android:textColor="#2E4C71"
android:gravity="center"/>
<Button
android:id="@+id/dolphinMinusButton"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_toRightOf="@+id/dolphinTitle"
android:layout_marginLeft="30dip"
android:text="@string/minus_button"
android:textSize="70dip"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="1dip"
android:background="@drawable/button_blue_square_selector"
android:textColor="#FFFFFF"
android:onClick="onClick"/>
<TextView
android:id="@+id/dolphinValue"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_marginLeft="15dip"
android:background="@android:drawable/editbox_background"
android:layout_toRightOf="@+id/dolphinMinusButton"
android:text="0"
android:textColor="#2E4C71"
android:textSize="50dip"
android:gravity="center"
android:textStyle="bold"
android:inputType="none"/>
<Button
android:id="@+id/dolphinPlusButton"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_toRightOf="@+id/dolphinValue"
android:layout_marginLeft="15dip"
android:text="@string/plus_button"
android:textSize="70dip"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="1dip"
android:background="@drawable/button_blue_square_selector"
android:textColor="#FFFFFF"
android:onClick="onClick"/>
</RelativeLayout>
기준:
<view class="com.example.MyQuantityBox"
android:id="@+id/dolphinBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:myCustomAttribute="@string/my_title"/>
따라서 사용자 지정 레이아웃을 원하지 않고 사용자 지정 보기를 원합니다(이 보기에 자식이 있으면 안 됩니다).
MyQuantityBox의 한 인스턴스에서 다른 인스턴스로 변경할 수 있는 유일한 것은 제목입니다.(마지막 XML 행에서와 같이) XML에 이 내용을 지정할 수 있으면 좋겠습니다.
이거 어떻게 해요?XML 파일에 RelativeLayout을 /res/layout에 넣고 MyBoxQuantity 클래스에서 부풀려야 합니까?만약 그렇다면 어떻게 해야 합니까?
감사합니다!
좀 늙었지만, 처브슨 더브즈의 대답을 토대로 내가 어떻게 할 것인지 공유하려고 생각했습니다.사용합니다.FrameLayout단일 보기를 포함하고 xml에서 보기를 부풀리는 데 사용되므로 문서를 참조하십시오.
코드는 다음과 같습니다.
public class MyView extends FrameLayout {
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public MyView(Context context) {
super(context);
initView();
}
private void initView() {
inflate(getContext(), R.layout.my_view_layout, this);
}
}
xml에서 확장하여 사용자 지정 뷰(복합 뷰)를 만드는 간단한 데모입니다.
attrs.xml
<resources>
<declare-styleable name="CustomView">
<attr format="string" name="text"/>
<attr format="reference" name="image"/>
</declare-styleable>
</resources>
CustomView.kt
class CustomView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
ConstraintLayout(context, attrs, defStyleAttr) {
init {
init(attrs)
}
private fun init(attrs: AttributeSet?) {
View.inflate(context, R.layout.custom_layout, this)
val image_thumb = findViewById<ImageView>(R.id.image_thumb)
val text_title = findViewById<TextView>(R.id.text_title)
val ta = context.obtainStyledAttributes(attrs, R.styleable.CustomView)
try {
val text = ta.getString(R.styleable.CustomView_text)
val drawableId = ta.getResourceId(R.styleable.CustomView_image, 0)
if (drawableId != 0) {
val drawable = AppCompatResources.getDrawable(context, drawableId)
image_thumb.setImageDrawable(drawable)
}
text_title.text = text
} finally {
ta.recycle()
}
}
}
custom_graphics.xml
우리는 사용해야 합니다.merge대신 여기서ConstraintLayout왜냐면
사용하면ConstraintLayout여기, 레이아웃 계층 구조는ConstraintLayout->ConstraintLayout->ImageView+TextView=> 중복이 1개 있습니다.ConstraintLayout=> 성능이 그다지 좋지 않음
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:parentTag="android.support.constraint.ConstraintLayout">
<ImageView
android:id="@+id/image_thumb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="ContentDescription"
tools:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="@id/image_thumb"
app:layout_constraintStart_toStartOf="@id/image_thumb"
app:layout_constraintTop_toBottomOf="@id/image_thumb"
tools:text="Text" />
</merge>
사용. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<your_package.CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f00"
app:image="@drawable/ic_android"
app:text="Android" />
<your_package.CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0f0"
app:image="@drawable/ic_adb"
app:text="ADB" />
</LinearLayout>
결과
전체 코드 참조:
깃헙
네, 할 수 있습니다.상대 레이아웃, 선형 레이아웃 등은 보기이므로 사용자 정의 레이아웃은 사용자 정의 보기입니다.사용자 지정 레이아웃을 만들고 싶다면 고려해야 할 사항입니다.
복합 컨트롤을 만들려는 것입니다.RelativeLayout의 하위 클래스를 만들고 모든 구성 요소를 코드(TextView 등)로 추가하면 생성기에서 XML로부터 전달된 속성을 읽을 수 있습니다. 그러면 해당 속성을 제목 TextView에 전달할 수 있습니다.
http://developer.android.com/guide/topics/ui/custom-components.html
아래와 같이 LayoutInflater를 사용합니다.
public View myView() {
View v; // Creating an instance for View Object
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.myview, null);
TextView text1 = v.findViewById(R.id.dolphinTitle);
Button btn1 = v.findViewById(R.id.dolphinMinusButton);
TextView text2 = v.findViewById(R.id.dolphinValue);
Button btn2 = v.findViewById(R.id.dolphinPlusButton);
return v;
}
실제로 xml을 반복적으로 사용하는 경우에는 조금 주의가 필요하다는 것을 알게 되었습니다.예를 들어, 목록의 각 항목에 대해 표 행을 생성하려는 표가 있다고 가정합니다.xml을 설정했습니다.
my_table_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/myTableRow">
<ImageButton android:src="@android:drawable/ic_menu_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rowButton"/>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView" android:id="@+id/rowText"></TextView>
</TableRow>
그런 다음 코드를 사용하여 행당 한 번씩 생성하려고 합니다.행을 첨부할 상위 TableLayout myTable을 정의했다고 가정합니다.
for (int i=0; i<numRows; i++) {
/*
* 1. Make the row and attach it to myTable. For some reason this doesn't seem
* to return the TableRow as you might expect from the xml, so you need to
* receive the View it returns and then find the TableRow and other items, as
* per step 2.
*/
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.my_table_row, myTable, true);
// 2. Get all the things that we need to refer to to alter in any way.
TableRow tr = (TableRow) v.findViewById(R.id.profileTableRow);
ImageButton rowButton = (ImageButton) v.findViewById(R.id.rowButton);
TextView rowText = (TextView) v.findViewById(R.id.rowText);
// 3. Configure them out as you need to
rowText.setText("Text for this row");
rowButton.setId(i); // So that when it is clicked we know which one has been clicked!
rowButton.setOnClickListener(this); // See note below ...
/*
* To ensure that when finding views by id on the next time round this
* loop (or later) gie lots of spurious, unique, ids.
*/
rowText.setId(1000+i);
tr.setId(3000+i);
}
행 버튼 처리에 대한 명확한 간단한 예입니다.OnClickListener(이것)를 설정합니다. 프로그래밍 방식으로 작성된 단추는 OnClickListener를 참조하십시오.
서로 다른 접근 방식에서 동일한 방식을 지적하는 답변이 여러 개 있는데, 자바를 사용해도 서드파티 라이브러리를 사용하지 않고 아래가 가장 간단한 접근 방식이라고 생각합니다.
코틀린에서;
만들기values/attr.xml
<resources>
<declare-styleable name="DetailsView">
<attr format="string" name="text"/>
<attr format="string" name="value"/>
</declare-styleable>
</resources>
만들기layout/details_view.xml보기위해 파일을 작성합니다.
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_label"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.5"
tools:text="Label" />
<TextView
android:id="@+id/text_value"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.5"
tools:text="Value" />
</LinearLayout>
</merge>
정의 위젯 DetailsView.kt
import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.payable.taponphone.R
class DetailsView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
private val attributes: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.DetailsView)
private val view: View = View.inflate(context, R.layout.details_view, this)
init {
view.findViewById<TextView>(R.id.text_label).text = attributes.getString(R.styleable.DetailsView_text)
view.findViewById<TextView>(R.id.text_value).text = attributes.getString(R.styleable.DetailsView_value)
}
}
이상으로, 아래와 같이 앱 어디에서나 위젯을 호출할 수 있습니다.
<com.yourapp.widget.DetailsView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:text="Welcome"
app:value="Feb" />
Kotlin을 이용한 간단한 Custom View
프레임 레이아웃을 원하는 보기로 바꿈
/**
* Simple Custom view
*/
class CustomView@JvmOverloads
constructor(context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0)
: FrameLayout(context, attrs, defStyleAttr) {
init {
// Init View
val rootView = (getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater)
.inflate(R.layout.custom_layout, this, true)
val titleView= rootView.findViewById(id.txtTitle)
// Load Values from XML
val attrsArray = getContext().obtainStyledAttributes(attrs, R.styleable.CutsomView, defStyleAttr, 0)
val titleString = attrsArray.getString(R.styleable.cutomAttrsText)
attrsArray.recycle()
}
}
언급URL : https://stackoverflow.com/questions/4328838/create-a-custom-view-by-inflating-a-layout
'codememo' 카테고리의 다른 글
| jQuery에서 태그 이름을 제공할 수 있습니까? (0) | 2023.09.15 |
|---|---|
| MySQL에서 (flow) 쿼리의 (stock) 결과를 축적하는 방법 (0) | 2023.09.15 |
| 도커 컴포지트를 사용하는 PHP MARIADB PHPMYADMIN 문제 (0) | 2023.09.15 |
| gitlab 개인 저장소에서 npm 모듈 설치 (0) | 2023.09.15 |
| Spring Data JPA Repository에서 save() 후 반환된 인스턴스를 사용하는 이유는 무엇입니까? (0) | 2023.09.10 |
