LinearLayout上的setMinimumHeight()不起作用(在子类中)

时间:2013-03-07 04:40:04

标签: java android

我有一个衬里布局的子类(从XML自定义类型引用)我试图设置的最小高度不起作用,也没有设置布局。我需要以编程方式执行此操作...

为什么setMinimumHeight()没有工作?

注意:代码的复杂性可以忽略,它设置的值是70

public class SummaryDynamicSpacer extends LinearLayout {

    private static final String TAG = "mymeter-DynamicSpacer";
    int dpWidth = 0;
    int dpHeight = 0;

    public SummaryDynamicSpacer(Context context) {
        super(context);
        init(context);
    }

    public SummaryDynamicSpacer(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        DisplayMetrics metrics = new DisplayMetrics();
        ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics);

        //Set dpWidth, this is the key metric we use for scaling.
        float density  = getResources().getDisplayMetrics().density;
        float dpWidth  = metrics.widthPixels / density;
        float dpHeight  = metrics.heightPixels / density;
        this.dpWidth = (int)dpWidth;
        this.dpHeight = (int)dpHeight;

        Log.d(TAG, "zyxHeightDP: "+metrics.heightPixels / density);

        int id = this.getId();
        int increment = 10;
        int adjustment = getValueForLargeDevice(increment);


        if(id == R.id.betweenAddressAndTopBox) {
            doBetweenAddressAndTopBox(context, adjustment);
        } else if(id == R.id.betweenTopAndAddress) {
            doBetweenTopAndAddress(context, adjustment);
        }

        Log.d(TAG, "SettingHeight: "+adjustment);
        this.setBackgroundColor(Color.BLUE);
    }

    private void doBetweenAddressAndTopBox(Context context, int adjustment) {
        this.setMinimumHeight(adjustment);
    }

    private void doBetweenTopAndAddress(Context context, int adjustment) {
        this.setMinimumHeight(adjustment);
    }

    private int getValueForLargeDevice(double increment) {
        int largeDeviceAdjustment = 0;
        if(dpHeight > 750) {
            int difference = (int)dpHeight - 750;
            int interval = 25;
            int iterations =  (difference / interval) > 12 ? 12 : (difference / interval);
            int increasePerInterval = 0;
            for(int i=0; i < iterations; i++) {
                largeDeviceAdjustment += increment;
            }
            return largeDeviceAdjustment;
        } else {
            return 1;
        }
    }



}

1 个答案:

答案 0 :(得分:1)

我认为您的代码很好。您的布局可能存在问题。您是否按如下方式设置了布局?

    <com.example.dynamicspacerlayout.SummaryDynamicSpacer
      android:id="@+id/betweenAddressAndTopBox"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >

另见does minHeight do anything?

运行代码的GalaxyNexus Screen Shot

enter image description here

Nexus7屏幕截图

enter image description here

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:hint="Top Text">

        <requestFocus />
    </EditText>

<com.example.dynamicspacerlayout.SummaryDynamicSpacer
    android:id="@+id/betweenAddressAndTopBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="@android:color/white"
         />
</com.example.dynamicspacerlayout.SummaryDynamicSpacer>

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" 
    android:hint="Bottom Text"
    />

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            int minimumHeight = findViewById(R.id.betweenAddressAndTopBox).getHeight();
            TextView text = (TextView) findViewById(R.id.textView1);
            text.setText("Height: " + minimumHeight);
//              text.setVisibility(minimumHeight > 1 ? View.VISIBLE : View.GONE);
        }
    }, 1000);

}
}

希望有帮助!