对于微调项自定义布局自定义、布局

2023-09-07 09:34:26 作者:珴只是一个孩纸而已

我已经警告对话框中的微调。我想,以减少微调的项目之间的填充,所以我实现了以下内容:

I have a spinner within alert dialog. I wanted to reduce padding between spinner items and hence I implemented following:

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>

活动code包含以下内容:

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长,所以单选按钮不应该被显示在微调。我怎样才能删除它?

Now as you can see in below screenshot, radio button is getting displayed on spinner which is actually a part of spinner_row.xml. Note that textview width is 200dp while spinner is only 130dp long, so that radio button should not have been displayed on spinner. How can I remove it?

此外,当我点击任何微调的项目,微调弹出没有得到消失预期。(注意,所有3的复选框在微调的项目清单检查)。 setOnItemSelectedListener 是没有得到所谓的关于项目的点击。

Also, when I click any of the spinner item, spinner pop-up doesn't get disappeared as expected.(note all 3 check-boxes are checked in spinner items list). setOnItemSelectedListener is not getting called on item click.

任何帮助AP preciated。

Any help appreciated.

按法鲁克的建议下,我尽了code和下面的结果。

As per farrukh's suggestion, I tried his code and following is the result.

推荐答案

我有这样的

这些$ C $ XML了C

with these code of xml

XML的适配器名为spinadapt.xml

xml for adapter named spinadapt.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

and main layout named 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 code是一个名为MainActivity.java类

and java code is class named 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适配器类

and adapter class named 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;
        }
}

这是工作完美

希望这将有助于