在我的MainActivity代码中更改微调框的大小

时间:2018-08-07 04:56:31

标签: android

我知道如何更改xml代码中元素的大小,但是可以在Main activity(java)代码中进行更改吗?我已经将代码编程为代码中重复的微调器出现在屏幕上,但我希望它们的宽度较小(当前它们跨过屏幕的宽度)。下面是我的create.java代码。

谢谢!

create.java

public class create extends AppCompatActivity {


    Button buttontest;
    private LinearLayout mLinearLayout;

    private ArrayList<Spinner> mSpinners;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        mSpinners = new ArrayList<>();

        mLinearLayout = findViewById(R.id.my_linearLayout);

        //mLinearLayout.addView(makeSpinner());    // First spinner

        Button duplicateSpinner = findViewById(R.id.bt_duplicate);
        duplicateSpinner.setOnClickListener(new View.OnClickListener() {




            @Override
            public void onClick(View view) {

                Spinner spinner = makeSpinner();
                mLinearLayout.addView(spinner);
                // Add another spinner



            }
        });

       // Button getSpinner = findViewById(R.id.bt_getSpinner); //code for getspinner
        //getSpinner.setOnClickListener(new View.OnClickListener() {
          //  @Override
           // public void onClick(View view) {

           //     for (int i = 0; i < mSpinners.size(); i++) {      // Read all spinners
           //         Spinner spinner = mSpinners.get(i);
           //         Log.i("TAG", spinner.getSelectedItem().toString());
          //      }
         //   }
      //  });
    }


    private Spinner makeSpinner() {
        //opens csv
        InputStream inputStream = getResources().openRawResource(R.raw.shopitems);
        CSVFile csvFile = new CSVFile(inputStream);
        List<String> itemList = csvFile.read();

        //Create new spinner
        Spinner spinner = new Spinner(this, Spinner.MODE_DROPDOWN);

        // Setup layout
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        spinner.setLayoutParams(layoutParams);
        MyListAdapter adapter = new MyListAdapter(this, R.layout.listrow, R.id.txtid, itemList);


        spinner.setAdapter(adapter);

        //Add it to your list of spinners so you can retrieve their data when you click the getSpinner button
        mSpinners.add(spinner);
        return spinner;
    }


    private class CSVFile {
        InputStream inputStream;

        public CSVFile(InputStream inputStream) {
            this.inputStream = inputStream;
        }

        public List<String> read() {

            List<String> resultList = new ArrayList<String>();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            try {
                String line;
                while ((line = reader.readLine()) != null) {
                    String[] row = line.split(",");
                    resultList.add(row[1]);
                }
            } catch (IOException e) {
                Log.e("Main", e.getMessage());
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    Log.e("Main", e.getMessage());
                }
            }
            return resultList;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试一下

  LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)layout.getLayoutParams();
    layoutParams.height = 250;//pixels
    layoutParams.width = 250;//pixels
    spinner.setLayoutParams(layoutParams);

dp转换为Px

public static int convDpToPx(Context context, float dp) {
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics);
}