0

すべての子を保持するカスタム コード レイアウトがあります。コードは次のとおりです。

public abstract class ItemView extends LinearLayout{
    private ImageView icon;//cell icon
    public static final int iconID=12345;
    private Button accessorButton;//this will not be button ?
    public static final int accessorID=54321;



    public ItemView(Context context) {
        super(context);
    }

    public  void arrange(){

    }
    public  void setView(Context context){  
        int l=super.getLeft();
        int r=super.getRight();
        int t=super.getTop();
        int b=super.getBottom();
        icon=new ImageView(context);
        icon.setImageResource(R.drawable.star);
        addView(icon);
        accessorButton=new Button(context);
        accessorButton.setText("accessor");
        addView(accessorButton);
       // icon.layout(l+5, t+5, l+100, b-5);
        //accessorButton.layout(r-100, t+5, r-10, b-5);


    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub
        super.onLayout(changed, l, t, r, b);


    }


}

子レイアウトがあります。そのうちの1つは次のとおりです。

public class ItemPlainView extends ItemView{

    private TextView text;

    public ItemPlainView(Context context) {
        super(context);
        setView(context);
    }
    public void setView(Context context){
      super.setView(context);
       text=new TextView(context);
       text.setText("plain view text");
       addView(text);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub
    super.onLayout(changed, l, t, r, b);


    }
    public TextView getText() {
        return text;
    }
    public void setText(TextView text) {
        this.text = text;
    }

    @Override
    public void arrange() {
        // TODO Auto-generated method stub

    }
}

コードを実行すると、自然にスター アイコン、ボタン、そして TextView が表示されます。オブジェクトを置き換えてサイズを変更する必要があります。この例では、中央の一番左の TextView に星のアイコンを配置し、一番右側にボタンを配置する必要があります。この階層的なビューを考えると、どうすればそれを行うことができますか?

ありがとう

4

2 に答える 2

1

@dymmeh はまさにそのとおりです。実行しているすべてのことは、次のように main.xml に含めることができます。

@Steve提案どおりに別のものを追加しました。前または後のTextViewいずれかを選択する必要がありますTextViewORTextViewImageViewOR

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

    <TextView
        android:drawableLeft="@drawable/star"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="plain view text"
        android:weight="1"
        />

    <<--OR-->>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/star"
        android:weight="1"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="plain view text"
        android:weight="1"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="accessor"
        android:weight="1"
        />     
</LinearLayout>

その後、Java クラスはafterを呼び出すだけsetContentView(R.layout.main);で済みます。onCreatesuper.onCreate();

あなたが持っていたものよりもはるかに簡単です。これらのアイテムを好きなように簡単にアレンジすることもできます。LinearLayoutを参照してください。 TableLayoutも使用できますが、ほとんどの状況では複雑すぎると思います。

于 2011-06-16T15:39:41.530 に答える
0

これは、XML レイアウトを使用するとはるかに簡単です。私はそれを使用することをお勧めします。ただし、本当にコードで行う必要がある場合は、中央の TextView を設定して、layout_weight = 1 のレイアウト パラメータを設定できます。

于 2011-06-16T15:42:00.827 に答える