简体   繁体   English

LayoutParams 使我的 AppCompatTextView 消失

[英]LayoutParams is making my AppCompatTextView to disappear

I have the following ContstraintLayout:我有以下 ContstraintLayout:

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/my_text"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:text="@string/txt_string"

            app:layout_constraintBottom_toBottomOf="@id/one"
            app:layout_constraintEnd_toStartOf="@id/two"
            app:layout_constraintStart_toEndOf="@id/three"
            app:layout_constraintTop_toTopOf="@id/four"
            android:layout_marginTop="@dimen/margin_dim"
            />
    </androidx.constraintlayout.widget.ConstraintLayout>

I want to change the marginTop value in code:我想更改代码中的 marginTop 值:

This is what I have done but the AppCompatTextView is disappearing:这就是我所做的,但 AppCompatTextView 正在消失:

        AppCompatTextView mytext;
    
    private void onCreate(@Nullable Bundle savedInstanceState) {
    
          super.onCreate(savedInstanceState);
            setContentView(R.layout.my_layout);
    
            mytext = findViewById(R.id.my_text);
    
ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(0,10,0,0);
mytext.setLayoutParams(params);


}

Try to modify the existing layout params instead of creating new ones.尝试修改现有布局参数而不是创建新布局参数。 By creating new params, you are likely losing the other information you have defined in your XML, such as your constraints.通过创建新的参数,您可能会丢失您在 XML 中定义的其他信息,例如您的约束。 I'm going from memory, but I would try something like this:我从记忆中走出来,但我会尝试这样的事情:

ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) mytext.getLayoutParams();
params.setMargins(0,10,0,0);
mytext.setLayoutParams(params);

You may also be able to clone the existing layoutparams instead of modifying the existing ones:您也可以克隆现有的布局参数而不是修改现有的布局参数:

final ConstraintLayout.LayoutParams current = 
  (ConstraintLayout.LayoutParams) mytext.getLayoutParams();
final ConstraintLayout.LayoutParams params = 
  new ConstraintLayout.LayoutParams(current);
params.setMargins(0,10,0,0);
mytext.setLayoutParams(params);

Edit: Also, consder that setMargins accepts dimensions as pixels as an argument, not density-independent pixels.编辑:另外,请注意setMargins接受尺寸作为像素作为参数,而不是与密度无关的像素。 This means that if you just set an int value like 10 , then your layout will not scale well for different android devices.这意味着如果你只是设置一个 int 值,比如10 ,那么你的布局将无法很好地适应不同的 Android 设备。 You can convert the value to DP yourself , or define the value in xml and use Resources::getDimensionPixelOffest .您可以自己将该值转换为 DP,或者在xml中定义该值并使用Resources::getDimensionPixelOffest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM