8

私はAndroidの世界に不慣れです....誰かが私を修正した場合、それは大きな助けになります...私は以下のコードを間違って何をしていますか...

  • 要件:アプリケーションアクティビティ内で同じビューを使用できるように、(xmlレイアウトファイルを使用して)カスタムビューを作成する必要があります。ここでは、私が取り組んでいるサンプルコードを使用します。

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>

拡張ビュークラス...コード...

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);
    }

アクティビティmain.xmlファイル

<?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>

アクティビティクラス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);
}
}
4

4 に答える 4

6

少し間違えたと思いますがmycustomTextview、レイアウトを膨らませるときは、ViewGroupも渡す必要があります。この行を使用できます。

mView = inflater.inflate(R.layout.cutomviewxml, this);
于 2014-01-07T11:37:48.417 に答える
4

カスタムビュー内のビューにアクセスするには、onFinishInflate()メソッドをオーバーライドし、findViewById()を使用します。当面の問題の解決策は次のとおりです。

public class mycustomTextview extends LinearLayout {

    Context mycontext;
    private TextView textView1;

    public mycustomTextview(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mycontext = context;
    }

    @Override
    protected void onFinishInflate() {
         super.onFinishInflate();
         textView1=(TextView) findViewById(R.id.textView1);
    }
}
于 2013-02-16T19:23:03.673 に答える
3

xmlでこれを使用します:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <yourpackagename.viewclassname android:id="@+id/myView1" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content"></yourpackagename.viewclassname>
</LinearLayout>
于 2012-06-11T07:09:17.787 に答える
1
public class ExampleView extends FrameLayout {

        ImageView mImage;

        public ExampleView(Context context) {
            super(context);
            init(context);
        }

        public void init(Context context) {
            View view = inflate(context, R.layout.my_view_layout,this);
            view.findViewById(R.id.image)
        }
}
于 2017-08-08T11:04:47.383 に答える