2017-10-06 3 views
2

Ich möchte die Größe meines linearen Layouts in meinem Programm nicht in der XML-Datei selbst ändern. Ich bin ein Anfänger und möchte auch meine Variable außerhalb einer Methode (public) deklarieren und in der Methode initialisieren, so wie ich es mit EditText und dem LinearLayout gemacht habe.Android Studio ändern LinearLayout Größe

Ich habe versucht, es auf diese Weise, aber es sagt:

Error:(47, 81) error: incompatible types: android.view.ViewGroup.LayoutParams cannot be converted to android.widget.LinearLayout.LayoutParams 

Aber ich weiß nicht, was mit diesem Fehler zu tun :(

My-Code so weit:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{ 


    public EditText input_chat; 
    public LinearLayout layout_chat_input; 
    public LinearLayout.LayoutParams layout_chat_input_params; 

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

     Toast.makeText(getApplicationContext(),"onCreate",Toast.LENGTH_SHORT).show(); 

     input_chat = (EditText) findViewById(R.id.input_chat); 
     layout_chat_input = (LinearLayout) findViewById(R.id.layout_chat_input); 
     layout_chat_input_params = new LinearLayout.LayoutParams(layout_chat_input.getLayoutParams()); 

     input_chat.addTextChangedListener(new TextWatcher() 
     { 

      public void onTextChanged(CharSequence s, int start, int before, int count) 
      { 
       if (input_chat.getLineCount() > 0 && input_chat.getLineCount() < 5) 
       { 

        layout_chat_input_params = layout_chat_input.getLayoutParams(); 
        layout_chat_input.setLayoutParams(); 
       } 
      } 

      public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 
      public void afterTextChanged(Editable s) {} 
     }); 
    } 
} 

Antwort

2

Try :

public void onTextChanged(CharSequence s, int start, int before, int count){ 

    if (input_chat.getLineCount() > 0 && input_chat.getLineCount() < 5){ 
     layout_chat_input.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
    } 
} 

ODER

layout_chat_input.setLayoutParams(layout_chat_input_params);