Tôi mới làm quen với thế giới Android .... nó sẽ là một trợ giúp tuyệt vời nếu ai đó sửa tôi ... tôi đang làm gì sai mã bên dưới ...Mã mẫu mở rộng Xem lớp và sử dụng tệp xml bố cục
- Yêu cầu: Cần tạo Chế độ xem tùy chỉnh (sử dụng tệp bố cục xml) để sử dụng cùng một chế độ xem bên trong trong hoạt động ứng dụng của tôi. Ở đây tôi đi với đoạn code mẫu mà tôi đang làm việc trên,
cutomviewxml.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Extended Xem lớp ... mã ...
mycustomTextview.java
public class mycustomTextview extends View {
private View mView;
Context mycontext;
public mycustomTextview(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
this.mycontext = context;
LayoutInflater inflater;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = inflater.inflate(R.layout.cutomviewxml, null);
}
Hoạt động main.xml tập tin
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.motorola.mycustomTextview
android:id="@+id/customtextview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_marginLeft="143dp"
android:layout_marginTop="69dp"
android:layout_toRightOf="@+id/textView1" />
</RelativeLayout>
Hoạt động lớp sample.java ..
public class sample extends Activity{
private static final String LOG_TAG = "sampleActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(LOG_TAG,"OnCreate of sampleActivity");
setContentView(R.layout.main);
}
}
+1. Cảm ơn bạn, nó thực sự giúp đỡ. Tôi đã điên lên này :) –