2015-06-19 2 views
0

Я написал программу для Android. в правой руке есть 4 маленьких зеленых ящика (текстовый вид). Я устанавливаю их как обертывание, чтобы динамически изменять высоту и покрывать весь макет, и если я добавлю дополнительный текст, уменьшу высоту.Высота текстового вида не изменяется динамически

Graphic:

Graphic

layout_file.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal" > 

<LinearLayout 
    android:layout_width="0dip" 
    android:layout_height="300dip" 
    android:orientation="vertical" android:layout_weight="0.75 "> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" android:layout_weight="0.25"> 

     <TextView 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:layout_margin="2dip" 
      android:layout_weight="0.25" 
      android:background="#00ffff" /> 

     <TextView 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:layout_margin="2dip" 
      android:layout_weight="0.25" 
      android:background="#00ff00" /> 
    </LinearLayout> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_margin="2dip" 
     android:background="#00ffff" android:layout_weight="0.25"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_margin="2dip" 
     android:background="#00ffff" android:layout_weight="0.25"/> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dib" android:layout_weight="0.25"> 

     <TextView 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:layout_margin="2dip" 
      android:layout_weight="0.2" 
      android:background="#00ffff" /> 

     <TextView 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:layout_margin="2dip" 
      android:layout_weight="0.7" 
      android:background="#00ff00" /> 
    </LinearLayout> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="0dip" 
    android:layout_height="300dip" 
    android:orientation="vertical" android:layout_weight="0.25"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="120dip" 
     android:layout_margin="2dip" 
     android:background="#00ff00" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="2dip" 
      android:background="#00ff0b" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="2dip" 
      android:background="#00ff0b" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="2dip" 
      android:background="#00ff0b" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="2dip" 
      android:background="#00ff0b" /> 


    </LinearLayout> 

</LinearLayout> 


Java-код:

package abc.sara.app.mystartu1; 

    import android.app.Activity; 
    import android.os.Bundle; 

    public class mystartup1 extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    } 
} 
+0

Где ваш код Java? –

+0

У вас есть вложенные вложенные вложенные веса, вам нужно переосмыслить свой макет. Я бы начал с этого – zgc7009

+0

@PranitBankar, какой java-код? – abcsd

ответ

0

Ну окей я наконец-то нашел вашу проблему. Постарайтесь играть больше с весами и TextViews.

  • Установите все ваши права, зеленые TextViews к android:layout_height="match_parent"
  • Установите все веса в android:layout_weight="1"
  • Установите самый высокий в android:layout_weight="0.7"

Теперь он должен выглядеть так:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="2dp" 
     android:background="#00ff0b" 
     android:layout_weight="0.7" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="2dp" 
     android:background="#00ff0b" 
     android:layout_weight="1" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="2dp" 
     android:background="#00ff0b" 
     android:layout_weight="1" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="2dp" 
     android:background="#00ff0b" 
     android:layout_weight="1" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="2dp" 
     android:background="#00ff0b" 
     android:layout_weight="1" 
     android:id="@+id/textView" /> 

    </LinearLayout> 
+0

да, вы можете видеть, что в конце кода у меня есть для textveiw и в каждом из них я устанавливаю android: layout_height = "wrap_content" – abcsd

+0

Я отредактировал свой ответ. Пожалуйста, оцените его и дайте мне отзыв. – 8m47x

+0

Спасибо, мне это очень помогло. – abcsd

0

В вашей деятельности:

TextView textview; 
textview = (TextView)findviewbyid(R.id.textview1); 
LinearLayout.LayoutParams Params1 = new LinearLayout.LayoutParams(25,50); 
textview .setLayoutParams(Params1); 


//try this 
0

сначала получите динамическую разметку

LinearLayout layout = (LinearLayout)findViewById(R.id.YOUD VIEW ID); 
ViewTreeObserver vto = layout.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
     this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
     int width = layout.getMeasuredWidth(); 
     int height = layout.getMeasuredHeight(); 

    } 
}); 

затем изменить высоту TextView с помощью логики, реализующей для расчета и подходит к экрану, как:

LayoutParams params = textView.getLayoutParams(); 
params.height = 70; 
textView.setLayoutParams(params); 
Смежные вопросы