3

我有一个衬垫布局的子类(从 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;
        }
    }



}
4

1 回答 1

1

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

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

看到这minHeight 也有什么作用吗?

运行您的代码的 GalaxyNexus 屏幕截图

在此处输入图像描述

Nexus7 屏幕截图

在此处输入图像描述

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

}
}

希望有帮助!

于 2013-03-17T20:20:09.883 回答