Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 线性布局上的setMinimumHeight()不是';t工作(在子类中)_Java_Android - Fatal编程技术网

Java 线性布局上的setMinimumHeight()不是';t工作(在子类中)

Java 线性布局上的setMinimumHeight()不是';t工作(在子类中),java,android,Java,Android,我有一个线性布局的子类(从XML自定义类型引用),我试图设置的最小高度不起作用,也无法设置布局。我需要按程序做这件事 为什么setMinimumHeight()不起作用 注意:代码的复杂性可以忽略,它设置的值是70 public class SummaryDynamicSpacer extends LinearLayout { private static final String TAG = "mymeter-DynamicSpacer"; int dpWidth = 0;

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



}
public类摘要dynamicspacer扩展了LinearLayout{
私有静态最终字符串标记=“mymeter DynamicSpacer”;
int dpWidth=0;
int dpHeight=0;
公共摘要DynamicAcer(上下文){
超级(上下文);
init(上下文);
}
公共摘要DynamicPacer(上下文上下文、属性集属性){
超级(上下文,attrs);
init(上下文);
}
私有void init(上下文){
DisplayMetrics=新的DisplayMetrics();
((活动)上下文).getWindowManager().getDefaultDisplay().getMetrics(度量);
//设置dpWidth,这是我们用于缩放的关键指标。
浮动密度=getResources().getDisplayMetrics().density;
float dpWidth=metrics.widthPixels/密度;
浮点dpHeight=metrics.height像素/密度;
this.dpWidth=(int)dpWidth;
this.dpHeight=(int)dpHeight;
Log.d(标签“zyxHeightDP:+metrics.heightPixels/density);
int id=this.getId();
int增量=10;
int调整=大型设备的GetValue(增量);
if(id==R.id.between-dressandtopbox){
地址和机顶盒之间的数据(上下文、调整);
}else if(id==R.id.betweenTopAndAddress){
dobetween-to和address(上下文、调整);
}
日志d(标签“设置高度:”+调整);
这个.setBackgroundColor(Color.BLUE);
}
Address和TopBox之间的私有无效数据(上下文,整数调整){
此参数设置最小高度(调整);
}
Top和Address之间的私有void dob(上下文,整数调整){
此参数设置最小高度(调整);
}
大型设备专用int getValueForLargeDevice(双倍增量){
内部大型设备调整=0;
如果(dpHeight>750){
int差=(int)dpHeight-750;
整数区间=25;
int迭代=(差异/间隔)>12?12:(差异/间隔);
int increasePerInterval=0;
对于(int i=0;i
我认为你的代码很好。您的布局可能有问题。您是否按如下方式设置了布局

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

希望有帮助

没有应用高度有什么问题!我已经将XML中的高度设置为1dp,但这一点没有改变。。根据,不能保证视图能够达到此最小高度(例如,如果其父布局限制视图的可用高度较低)。你也可以发布布局文件吗?应该与包裹内容一起工作,如果父级允许这样的高度。显示布局。是否尝试将其设置为0dp?它应该会从屏幕上消失。如果0dp起作用,则设置尺寸代码起作用。先试试这个,然后我再提出别的建议。
<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>
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);

}
}