Android Studio 3.1,Java 1.8,Gradle
4.1。
我使用minVersion = 15
我想在文件profile_category_active.xml中以编程方式设置GridConstrainLayout
的宽度
。
所以......
这是我的主要XML
<android.support.constraint.ConstraintLayout
android:id="@+id/categoriesContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/nameContainer"
app:layout_constraintTop_toBottomOf="@+id/birthDateContainer">
<GridLayout
android:id="@+id/categoriesGridContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/place_holder_color"
app:layout_constraintEnd_toEndOf="@+id/categoriesContainer"
app:layout_constraintStart_toStartOf="@+id/categoriesContainer"
app:layout_constraintTop_toBottomOf="@+id/categoriesContainer" />
</android.support.constraint.ConstraintLayout>
此处 profile_category_active.xml :
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/profileCategoryContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/profile_category_active_bg">
<TextView
android:id="@+id/categoryNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginEnd="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:ellipsize="end"
android:maxLines="1"
android:text="TextView"
android:textColor="#292929"
android:textSize="15sp"/>
</android.support.constraint.ConstraintLayout>
这里有活动代码:
public class ProfileActivity extends AppCompatActivity {
private static final String TAG = ProfileActivity.class.getName();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ProfileBinding profileBinding = DataBindingUtil.setContentView(this, R.layout.profile);
GridLayout categoriesGridContainer = findViewById(R.id.categoriesGridContainer);
categoriesGridContainer.setColumnCount(columnCount);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int index = 0; index < 13; index++) {
View profileCategoryActive = inflater.inflate(R.layout.profile_category_active, null, false);
categoriesGridContainer.addView(profileCategoryActive);
ConstraintLayout profileCategoryContainer = profileCategoryActive.findViewById(R.id.profileCategoryContainer);
ConstraintLayout.LayoutParams profileCategoryContaineParams = (ConstraintLayout.LayoutParams) profileCategoryContainer.getLayoutParams();
profileCategoryContaineParams.width = (int) AndroidUtil.dpToPx(this, categoryItemWidth);
}
}
}
但是在运行时我得到错误:
FATAL EXCEPTION: main
Process: com.myproject.android.customer.debug, PID: 15496
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myproject.android.customer.debug/com.myproject.android.customer.ui.ProfileActivity}: java.lang.ClassCastException: android.widget.GridLayout$LayoutParams cannot be cast to android.support.constraint.ConstraintLayout$LayoutParams
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassCastException: android.widget.GridLayout$LayoutParams cannot be cast to android.support.constraint.ConstraintLayout$LayoutParams
at com.myproject.android.customer.ui.ProfileActivity.init(ProfileActivity.java:76)
为什么这行错误?
ConstraintLayout.LayoutParams profileCategoryContaineParams = (ConstraintLayout.LayoutParams) profileCategoryContainer.getLayoutParams();
profileCategoryContainer是ConstraintLayout
(不是GridLayout
)
答案 0 :(得分:1)
试试这个
ConstraintLayout.LayoutParams clpcontactUs = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
contactUs.setLayoutParams(clpcontactUs);