在我的Android Studio项目中,我有一个带有约束布局的片段。这是XML。
<android.support.constraint.ConstraintLayout
android:id="@+id/pinLayout"
android:layout_width="match_parent"
android:layout_height="250dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/topLayout">
<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/constraintLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="visible">
<TextView
android:id="@+id/numberTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:gravity="top"
android:text="00000000"
android:textColor="@color/appTintColor"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="@+id/constraintLayout2"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.4"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="@+id/confirmButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_corner_shape_tint"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="Confirm Pin"
android:textColor="@color/appMainColor"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/constraintLayout2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.4"
app:layout_constraintStart_toStartOf="parent">
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/pinView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center"
android:text="-"
android:textColor="@color/appTintColor"
android:textSize="36sp" />
<TextView
android:id="@+id/pinView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center"
android:text="-"
android:textColor="@color/appTintColor"
android:textSize="36sp" />
<TextView
android:id="@+id/pinView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center"
android:text="-"
android:textColor="@color/appTintColor"
android:textSize="36sp" />
<TextView
android:id="@+id/pinView4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center"
android:text="-"
android:textColor="@color/appTintColor"
android:textSize="36sp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
如您所见,“确认”按钮位于其父项的约束中心。
但是当我在Xperia z5 Compact中运行应用程序时,布局检查器显示按钮未居中。
为什么会发生以及如何解决?
注意,如果我要删除其父constarintLayout的numberTextView,该问题将消失。 注意,在我的代码中,我以编程方式将文本设置为numberTextView。如果我在注释该代码(即不以编程方式设置文本)时,按钮会正确绘制。
这是代码部分,我将文本设置为NumberTextView
private void performInitialLayout() {
fragmentViewHeight = fragmentView.getHeight();
ViewRelatedUtils.setHeightPercentage(topView, scrollView, 0.3);
ViewRelatedUtils.setHeightPercentage(pinLayout, scrollView, 0.4);
// here I am setting a text.
numberTextView.setText(number);
if (isProcessingPinConfirmation) showProcessing(); else hideProcessing();
wrapperView.requestFocus();
performedInitialLayout = true;
}
答案 0 :(得分:0)
请阅读我对这个问题的评论,尽管我对您的布局进行了一些更改,请查看
根据您的要求更改颜色。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//Type of View which is ROW(0) or HEADER(1)
int type = getItemViewType(position);
//If there is no View create it,
if (convertView == null) {
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (type) {
case ROW:
convertView = inflater.inflate(R.layout.activity_transaction_items, null);
break;
case HEADER:
convertView = inflater.inflate(R.layout.activity_transaction_header, null);
convertView.setBackgroundColor(Color.rgb(220,220,220));
default:
break;
}
}
switch (type){
case ROW:
ProfitTransactions transaction = (ProfitTransactions)getItem(position);
TextView tvDay = (TextView)convertView.findViewById(R.id.day);
TextView tvTID = (TextView)convertView.findViewById(R.id.tID);
TextView tvTotalPrice = (TextView)convertView.findViewById(R.id.totalPrice);
TextView tvTimeOrder = (TextView)convertView.findViewById(R.id.tvTime);
Log.d("transadapter","-----Test: " + Integer.parseInt(transaction.getDayOfOrder()));
tvDay.setText(transaction.getDayOfOrder()+ getDayNumberSuffix(Integer.parseInt(transaction.getDayOfOrder())));
tvTID.setText("TID: " + transaction.gettId());
tvTotalPrice.setText("+$"+String.format("%.2f", transaction.getTotalPrice()));
tvTimeOrder.setText("At: " + transaction.getTimeOfOrder());
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ProfitTransactions transaction = (ProfitTransactions)getItem(position);
//Create the Dialog Box
AlertDialog.Builder builder = new AlertDialog.Builder(c);
//Put message in the Dialog Box
builder.setMessage("Name: " + transaction.getName() + "\n" +
"Price: " + transaction.getPrice() + "\n" +
"Quantity: " + transaction.getQuantity() + "\n" +
"Total Price: " + transaction.getTotalPrice() + "\n"
)
//If user click Yes
.setPositiveButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
//Show the dialog after creating
AlertDialog dialog = builder.show();
}
});
break;
case HEADER:
String header = (String)getItem(position);
// from string to date
SimpleDateFormat inputFormat = new SimpleDateFormat("MM/yyyy");
Date date = null;
try {
date = inputFormat.parse(header);
} catch (ParseException e) {
e.printStackTrace();
}
// from date to string
SimpleDateFormat outputFormat = new SimpleDateFormat("MMM yyyy");
String dateTime = outputFormat.format(date);
TextView tvMonthYear = (TextView)convertView.findViewById(R.id.tvHeaderMonthYear);
tvMonthYear.setText(dateTime);
default:
break;
}
return convertView;
}