【发布时间】:2017-09-18 12:18:52
【问题描述】:
我需要动态地实现这一点。 ImageView 顶部是 TextView.
这是这个布局的代码。
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView2"
android:layout_width="200dp"
android:layout_height="200dp"
app:srcCompat="@drawable/button_facebook_login_pressed"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintHorizontal_bias="0.672"
app:layout_constraintVertical_bias="0.498" />
<TextView
android:id="@+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="@color/white"
android:textSize="20sp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="@+id/imageView2"
app:layout_constraintTop_toTopOf="@+id/imageView2"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="@+id/imageView2"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="@+id/imageView2" />
</android.support.constraint.ConstraintLayout>
我正在尝试动态执行此操作。这就是我所做的。
ConstraintLayout layout = (ConstraintLayout)view.findViewById(R.id.ll_seasonFragment);
ImageView imageView= new ImageView(getActivity().getApplicationContext());
TextView textView= new TextView(getActivity().getApplicationContext());
final float scale = getResources().getDisplayMetrics().density;
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
int margin = (int) (5 * scale + 0.5f);
layout.addView(textView);
layout.addView(imageView);
这是一个接一个地添加Views,我需要知道如何将它们绘制在另一个上。
编辑
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="368dp"
android:layout_height="551dp"
android:id="@+id/ll_seasonFragment"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:orientation="vertical"
>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
【问题讨论】:
-
什么是
ll_seasonFragment?你能发布那个 XML 吗? -
@Cheticamp : 我已经放了那个代码,里面没有项目
-
这有点清楚了。问题在于 LinearLayout 和您对它的使用。你需要 LinearLayout 还是想用 ConstraintLayout 做所有事情?您可以按照 Jyoti Sharma 的建议进行操作,并使用 RelativeLayout 而不是 LinearLayout。如果您不需要内部布局,最好只使用 ConstraintLayout 并且可以做到。
标签: android performance android-layout android-fragments android-constraintlayout