【问题标题】:TextView and ImageView stuck out of LinearLayoutTextView 和 ImageView 卡出 LinearLayout
【发布时间】:2020-04-27 16:25:12
【问题描述】:

我正在构建一个带有 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);


}
}

这是结果:

【问题讨论】:

    标签: java android android-layout


    【解决方案1】:

    所以不是你的元素在布局之外。问题是您的 ScrollView 有一个 top 约束top of parent

    将您的 ScrollView 顶部约束 (app:layout_constraintTop_toTopOf="parent") 更改为:

    app:layout_constraintTop_toBottomOf="@id/planets"
    

    由于你的布局容器是一个 ConstraintLayout 元素可以在它们之间重叠。

    【讨论】:

    • 嗨,Charls 先生,感谢您的回复。我只是替换了你说的但是没有用,元素的位置和以前一样。
    • 因此,如果这不能解决问题,那么以编程方式设置元素就会出现问题。尝试将文本视图直接设置为线性,以验证滚动视图确实位于行星文本视图下方。
    • 我添加了 TextView 但它停留在顶部。约束有问题吗?
    • 如果您将 ScrollView 的约束顶部设置为行星 TextView,然后在 LinearLayout 中添加一个 textView,则新的 textView 应该出现在行星 TextView 下方
    猜你喜欢
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多