簡體   English   中英

TextView 和 ImageView 卡在 LinearLayout 之外

[英]TextView and ImageView stuck out of LinearLayout

I'm building an android app with a TextView, a ScrollView and a LinearLayout, I want to add one TextView and an ImageView to this LinearLayout using Java but they both stay outside of the Layout.

這是我的活動 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 之外。

所以不是你的元素在布局之外。 問題是您的ScrollView對 parent 的頂部有一個頂部約束

將您的 ScrollView 頂部約束 (app:layout_constraintTop_toTopOf="parent") 更改為:

app:layout_constraintTop_toBottomOf="@id/planets"

由於您的布局容器是一個 ConstraintLayout 元素可以在它們之間重疊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM