首页 > 解决方案 > TextView 和 ImageView 卡出 LinearLayout

问题描述

我正在构建一个带有 TextView、ScrollView 和 LinearLayout 的 android 应用程序,我想使用 Java 向此 LinearLayout 添加一个 TextView 和一个 ImageView,但它们都位于布局之外。

这是我的活动 XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/galaxy"
tools:context=".Planet">

<TextView
    android:id="@+id/planets"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/purple"
    android:text="@string/planets"
    android:textAlignment="center"
    android:textColor="#000000"
    android:textSize="36sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.05" />

<ScrollView
    android:id="@+id/scroll"
    android:layout_width="409dp"
    android:layout_height="646dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0">

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

这是我用来生成和添加 TextView 和 ImageView 的代码:

public class MainActivity extends AppCompatActivity {

private ArrayList<Planet> planets = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Planet mercury = Planet.getPlanetFromResources(0, getResources());
    planets.add(mercury);
    addPlanetToUI(mercury);


}


public void addPlanetToUI(final Planet planet){

    int color = getResources().getColor(R.color.yellow);

    LinearLayout linear = findViewById(R.id.linear);
    linear.setOrientation(LinearLayout.VERTICAL);
    linear.setWeightSum(20f);

    LinearLayout.LayoutParams lp = new 
    LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
    LinearLayout.LayoutParams.WRAP_CONTENT);
    LinearLayout.LayoutParams lp1 = new 
    LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
    LinearLayout.LayoutParams.WRAP_CONTENT);
    LinearLayout.LayoutParams lp2 = new 
    LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
    LinearLayout.LayoutParams.WRAP_CONTENT);

    LinearLayout horizontal = new LinearLayout(this);
    horizontal.setWeightSum(6f);
    horizontal.setOrientation(LinearLayout.HORIZONTAL);
    lp.weight = 10f;
    horizontal.setLayoutParams(lp);

    ImageView iv = new ImageView(this);
    lp1.weight = .75f;
    iv.setImageDrawable(getDrawable(planet.getImgSmallResourceValues()));
    iv.setLayoutParams(lp1);


    TextView tv = new TextView(this);
    lp2.weight= .5f;
    tv.setText(planet.getName());
    tv.setBackgroundColor(color);
    tv.setTextSize(40);
    tv.setLayoutParams(lp2);

    horizontal.addView(iv);
    horizontal.addView(tv);
    linear.addView(horizontal);


}
}

这是结果:

ImageView 和 TextView 在 LinearLayout 之外。

标签: javaandroidandroid-layout

解决方案


So is not that your elements are outside of the layout. The problem is that your ScrollView has a top constraint to the top of parent.

Change your ScrollView top constraint (app:layout_constraintTop_toTopOf="parent") to:

app:layout_constraintTop_toBottomOf="@id/planets"

Since your layout container is a ConstraintLayout elements can overlap between them.


推荐阅读