سؤال

Width of layout in oncreate function, I need this size but your valor is 0. Here some of my code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 

    LinearLayout layout_principal=(LinearLayout)findViewById(R.id.pru);

    int mywidth=layout_principal.getWidth();
    LinearLayout.LayoutParams txt_sep_principal_Params=new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            mywidth/2);
         TextView txt_sep_principal=new TextView(this);
         txt_sep_principal.setLayoutParams(txt_sep_principal_Params);
         txt_sep_principal.setText("text of my code");
         TextView txt_sep_principal2=new TextView(this);
         txt_sep_principal2.setLayoutParams(txt_sep_principal_Params);
         txt_sep_principal2.setText("Text of my code");
    layout_principal.addView(txt_sep_principal);
    layout_principal.addView(txt_sep_principal2);


}
هل كانت مفيدة؟

المحلول

After this line

 LinearLayout layout_principal=(LinearLayout)findViewById(R.id.pru);

Try add these lines of code.

layout_principal.setOrientation(LinearLayout.VERTICAL);
layout_principal.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

نصائح أخرى

It's too early to get the size of layout in oncreate method

try this code :

@Override
 public void onWindowFocusChanged(boolean hasFocus) {
 // TODO Auto-generated method stub
 super.onWindowFocusChanged(hasFocus);
 //Here you can get the size!
   Toast.makeText(this,""+ Layout.getHeight(), Toast.LENGTH_LONG).show();
}

Look full code here :

LinearLayout layout_principal;
    int mywidth;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 

        layout_principal =(LinearLayout)findViewById(R.id.pru);


        LinearLayout.LayoutParams txt_sep_principal_Params=new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                mywidth/2);
             TextView txt_sep_principal=new TextView(this);
             txt_sep_principal.setLayoutParams(txt_sep_principal_Params);
             txt_sep_principal.setText("text of my code");
             TextView txt_sep_principal2=new TextView(this);
             txt_sep_principal2.setLayoutParams(txt_sep_principal_Params);
             txt_sep_principal2.setText("Text of my code");
        layout_principal.addView(txt_sep_principal);
        layout_principal.addView(txt_sep_principal2);
        }


     @Override
     public void onWindowFocusChanged(boolean hasFocus) {
     // TODO Auto-generated method stub
       super.onWindowFocusChanged(hasFocus);
      //Here you can get the size!
       mywidth=layout_principal.getWidth();
      }

The layout may not have been completely finished drawing. Try posting a runnable on it. Something like

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 

    LinearLayout layout_principal=(LinearLayout)findViewById(R.id.pru);
    layout_principal.post(new Runnable()
    {       
      public void run()
      {
       int mywidth=layout_principal.getWidth();
       LinearLayout.LayoutParams txt_sep_principal_Params=new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.MATCH_PARENT,
       mywidth/2);
       TextView txt_sep_principal=new TextView(this);
       ...
    }
 });

Edit

public class YourActivty extends Activity
{
     // These are declarations
     TextView tv;
     Button btn;

     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // these are initializations
        tv = (TextView) findViewById(R.id.someId);
        btn = (Button) findViewById(R.id.someOtherId);

See the comments in my code. Declaring them this way makes them available to the whole Activity. You can't initialize them before calling setContentView() and certainly not outside of a method. Obviously, you need to replace the sample Views I have used with your own.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top