activity_main.xml
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="icon change"/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="icon change"/>
<com.example.myapplication.Layout01
android:id="@+id/layout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
layout.xml
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor = "#eeeeee"
app:cardCornerRadius="10dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView"
android:layout_width="80dp"
android:layout_height="80dp"
android:padding="10dp"
app:srcCompat="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="title"
android:textSize = "26sp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="contents"
android:textColor="#00f"
android:textSize="16sp"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
Layout01.java
public class Layout01 extends LinearLayout {
ImageView imageView;
TextView textView;
TextView textView2;
public Layout01(Context context){
super(context);
init(context);
//생성자가 생성될때 inflation이 같이 되도록 하기위해
}//생성자
public Layout01(Context context, AttributeSet attrs){
super(context, attrs);
init(context);
}//xml속성을 활용하기위해
private void init(Context context){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.layout, root: this, attachToRoot: true);
imageView = findViewById(R.id.imageView);
textView = findViewById(R.id.textView);
textView2 = findViewById(R.id.textView2);
} //초기화작업을 생성자를 통해 하기위해 init method를 이용한다
public void setImage(int resId){
imageView.setImageResource(resId);
}
public void setTitle(String title){
textView.setText(title);
}
public void setComment(String comment){
textView2.setText(comment);
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
Layout01 layout01;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout01 = findViewById(R.id.layout01);
layout01.setImage(R.drawable.ic_launcher_foreground);
layout01.setTitle("hahahoho");
layout01.setComment("hihihihi");
Button btn = findViewById(R.id.btn1);
btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
layout01.setImage(R.drawable.icon);
}
});
Button btn2 = findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
layout01.setImage(R.drawable.icon);
}
});
}
}
'android studio' 카테고리의 다른 글
xml을 이용한 focus event (0) | 2019.08.12 |
---|---|
touch event, gesture event (0) | 2019.08.12 |
버튼 style변경(onDraw(), invalidate()) (0) | 2019.08.12 |
toast message, style (0) | 2019.08.11 |
inflation (0) | 2019.08.11 |