13

アラート ダイアログ内にスピナーがあります。スピナーアイテム間のパディングを減らしたかったので、以下を実装しました:

spinner_row.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="30dp"
    android:background="#fff" >

    <TextView
        android:id="@+id/tvCust"
        android:layout_width="200dp"
        android:layout_height="30dp"
        android:gravity="left|center_vertical"
        android:textColor="#000"
        android:textSize="15sp" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_alignParentRight="true" />

</RelativeLayout>

アクティビティ コードには次のものが含まれます。

spinner= (Spinner) dialog.findViewById(R.id.spinner);
String arr[] = { "1", "2", "3" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
CameraActivity.this, R.layout.spinner_row, R.id.tvCust,arr);
spinner.setAdapter(adapter);

下のスクリーンショットでわかるように、実際には の一部であるスピナーにラジオ ボタンが表示されていますspinner_row.xml。textview の幅は 200dp ですが、スピナーの長さはわずか 130dp であるため、ラジオ ボタンはスピナーに表示されるべきではありません。どうすれば削除できますか?

また、スピナー項目のいずれかをクリックしても、スピナー ポップアップが期待どおりに消えません (スピナー項目リストで 3 つのチェック ボックスがすべてオンになっていることに注意してください)。setOnItemSelectedListenerアイテムのクリックで呼び出されていません。

どんな助けでも感謝します。

スクリーンショット

編集 1

farrukhの提案に従って、私は彼のコードを試しました。結果は次のとおりです。

スクリーンショット

4

1 に答える 1

13

私はこれを持っています

ここに画像の説明を入力

この

ここに画像の説明を入力

これらのxmlのコードで

spinadapt.xml という名前のアダプターの 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="30dp"
android:background="#fff" >

<TextView
    android:id="@+id/tvCust"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:layout_toLeftOf="@+id/radioButton1"
    android:gravity="left|center_vertical"
    android:textColor="#000"
    android:textSize="15sp" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_alignParentRight="true" />

</RelativeLayout>

activity_main.xml という名前のメイン レイアウト

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" 
    android:hint="Select item"
    android:background="@drawable/spin"/>

</RelativeLayout>

そしてJavaコードはMainActivity.javaという名前のクラスです

public class MainActivity extends Activity
{
    Spinner sp;
    TextView tv;
    String[]  counting={"One","Two","Three","Four"};
    @Override
    protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            sp=new Spinner(this);
            tv=(TextView)findViewById(R.id.spinner1);
            tv.setOnClickListener(new OnClickListener()
                {                       
                    @Override
                    public void onClick(View v)
                        {
                            sp.performClick();
                        }
                });
            sp.setAdapter(new Adapter(MainActivity.this, counting));
            sp.setOnItemSelectedListener(new OnItemSelectedListener()
                {
                    @Override
                    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
                        {
                            tv.setText(counting[arg2]);
                        }
                    @Override
                    public void onNothingSelected(AdapterView<?> arg0)
                        {
                        }
                });
        }
}

および Adapter.java という名前のアダプタ クラス

public class Adapter extends BaseAdapter
{
    LayoutInflater inflator;
    String[] mCounting;

    public Adapter( Context context ,String[] counting)
        {
            inflator = LayoutInflater.from(context);
            mCounting=counting;
        }

    @Override
    public int getCount()
        {
            return mCounting.length;
        }

    @Override
    public Object getItem(int position)
        {
            return null;
        }

    @Override
    public long getItemId(int position)
        {
            return 0;
        }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
        {
            convertView = inflator.inflate(R.layout.spinadapt, null);
            TextView tv = (TextView) convertView.findViewById(R.id.tvCust);
            tv.setText(Integer.toString(position));
            return convertView;
        }
}

これは完璧に機能しています

これが役立つことを願っています

于 2013-07-01T15:27:31.433 に答える