文本视图的高度不会动态变化

时间:2015-06-19 12:10:49

标签: android xml

我写了一个android程序。在右手边有4个小绿框(文字视图)。我将它们的高度设置为包裹以动态更改高度并覆盖整个布局,如果我添加额外的textview,则高度会降低。

图形:

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);
  }
}

3 个答案:

答案 0 :(得分:0)

在您的活动中:

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


//try this 

答案 1 :(得分:0)

好吧,我终于找到了你的问题。尝试使用权重和TextViews更多地玩。

  • 将您所有正确的绿色TextView设置为EditTextPreference myEdTePref = (EditTextPreference) myPref; myEdTePref.getEditText().setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
  • 将所有权重设置为android:layout_height="match_parent"
  • 将最高设置为android:layout_weight="1"

现在看起来应该是这样的:

android:layout_weight="0.7"

答案 2 :(得分: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);