我正在尝试以编程方式创建一些视图以匹配我在XML布局中制作的模型,但尽管看似具有相同的属性,但它们的显示方式不同。
左侧的卡是使用下面的XML创建的,右侧的卡是使用代码创建的。正确的布局是左边的那个,我的代码在哪里错了?
XML(左图)
<LinearLayout
android:layout_width="wrap_content"
android:orientation="horizontal"
android:id="@+id/cardViewContainer"
android:layout_height="200dp">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/testCard">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="200dp"
android:minWidth="200dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST TITLE"
android:layout_weight="1"
android:layout_gravity="bottom"
android:gravity="center"
android:layout_margin="5px" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
代码(右图)
var layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
int cardMinDims = (int)(200 * scale + 0.5f);
TextView title = new TextView(this);
title.Text = "TEST TITLE";
title.Gravity = GravityFlags.Center;
var title_layoutParams = layoutParams;
title_layoutParams.Weight = 1;
title_layoutParams.Gravity = GravityFlags.Bottom;
title_layoutParams.SetMargins(5, 5, 5, 5);
LinearLayout layout = new LinearLayout(this);
layout.SetMinimumHeight(cardMinDims);
layout.SetMinimumWidth(cardMinDims);
layout.Orientation = Orientation.Vertical;
layout.AddView(title,title_layoutParams);
CardView card = new CardView(this);
card.AddView(layout, layoutParams);
LinearLayout cardViewContainer = FindViewById<LinearLayout>(Resource.Id.cardViewContainer);
cardViewContainer.AddView(card,layoutParams);
答案 0 :(得分:0)
在您的代码中重新使用您的LayoutParams
: -
即
var layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
var title_layoutParams = layoutParams;
现在,title_layoutParams
对象的引用指向layoutParams
的相同对象引用。
您之后使用相同的layoutParams
。
由于这些对象引用,您所做的任何更改都将应用于所有其他变量,因为它们指向同一对象。
我已经在代码中重新创建了演示,下面几乎与您的axml
相同,layout_width
和layout_height
只针对{CardView
进行了一些调整1}}和内部LinearLayout
使其按预期工作: -
我已经从下面的演示中省略了任何缩放: -
示例: - 强>
LinearLayout objLinearLayout_CardViewContainer = new LinearLayout(this);
objLinearLayout_CardViewContainer.Orientation = Orientation.Horizontal;
objLinearLayout_CardViewContainer.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, 400);
Android.Support.V7.Widget.CardView objCardView = new CardView(this);
objLinearLayout_CardViewContainer.AddView(objCardView, LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent);
LinearLayout objCardViewLinearLayout = new LinearLayout(this);
objCardViewLinearLayout.SetMinimumWidth(400);
objCardViewLinearLayout.SetMinimumHeight(400);
objCardView.AddView(objCardViewLinearLayout, CardView.LayoutParams.MatchParent, CardView.LayoutParams.MatchParent);
TextView objTextView = new TextView(this);
objTextView.Text = "TEST TITLE";
objTextView.SetTextColor(Color.Red);
objTextView.Gravity = GravityFlags.Center;
LinearLayout.LayoutParams objLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
objLayoutParams.Weight = 1;
objLayoutParams.Gravity = GravityFlags.Bottom;
objLayoutParams.SetMargins(5, 5, 5, 5);
objCardViewLinearLayout.AddView(objTextView, objLayoutParams);
然后您将获得类似于以下内容的输出: -