728x90
반응형
**Layout1**
package com.example.mylayout;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
public class Layout1 extends LinearLayout{
ImageView imageView;
TextView textView;
TextView textView2;
public Layout1(Context context) {
super(context);
init(context);
}
public Layout1(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}
public void init(Context context){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.layout1, this, true);// 우리가 만든 xml과 연결해라
imageView = findViewById(R.id.imageView);
textView=findViewById(R.id.textView);
textView2=findViewById(R.id.textView2);
}
public void setImageView(int resId){
imageView.setImageResource(resId);
}
public void setName(String name){
textView.setText(name);
}
public void setMoblie(String moblie){
textView2.setText(moblie);
}
}
**Layout1.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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_width="80dp"
android:layout_height="80dp"
app:srcCompat="@drawable/ic_launcher_foreground" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="이름"
android:textColor="@android:color/holo_red_dark"
android:textSize="30sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="전화번호"
android:textColor="@android:color/holo_green_light"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
그렇게 만들어진것에 대해서
activity_main.xml 안에 만든 리니어 레이아웃을 넣습니다.
**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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="이미지 바꾸기" />
<com.example.mylayout.Layout1
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</com.example.mylayout.Layout1>
</LinearLayout>
그리고 나서 MainActivity.java 소스를 작성합니다.
기존 레이아웃에서 버튼을 누르면 이미지가 바뀌는 형식으로요
**MainActivity.java**
package com.example.mylayout;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Layout1 layout1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout1 = findViewById(R.id.layout1);
layout1.setImageView(R.drawable.ic_launcher_foreground);
layout1.setName("Black");
layout1.setMoblie("010-0000-0000");
Button btn = findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
layout1.setImageView(R.drawable.profile);
}
});
}
}
버튼을 눌렀을때 저렇게 이미지가 바뀌는 모습이 보이면 성공입니다.
이번 다음에는 저렇게 여러개의 개인 정보가 담겼을때 구분을 할 수 있는 카드뷰에 대해서 실습을 진행합니다.
**layout1.xml**
파일에서 cardView로 감싸주시면 됩니다.
<?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="wrap_content"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#FFFFFF"
app:cardCornerRadius="10dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
>
<ImageViewA
android:id="@+id/imageView"
android:layout_width="80dp"
android:layout_height="80dp"
app:srcCompat="@drawable/ic_launcher_foreground" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="이름"
android:textColor="@android:color/holo_red_dark"
android:textSize="30sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="전화번호"
android:textColor="@android:color/holo_green_light"
android:textSize="24sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
728x90
반응형
'안드로이드(Android)' 카테고리의 다른 글
안드로이드 이론 빡공 21 (0) | 2020.07.06 |
---|---|
안드로이드 이론 빡공 20 (0) | 2020.07.03 |
안드로이드 이론 빡공 18 (0) | 2020.07.01 |
안드로이드 이론 빡공 17 (0) | 2020.06.29 |
안드로이드 이론 빡공 16 (0) | 2020.06.24 |
댓글