我是移动开发的新手,我正在尝试运行我的活动,并使用我的手机/模拟器查看我的布局结果。 在设计模式下,它看起来不错,但是当我在模拟器/手机上运行时,所有元素都是一个在另一个上面的。
我有这些文件:
activity_item_page.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:baselineAligned="false"
android:orientation="vertical"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<TextView
android:id="@+id/product_name"
android:layout_width="343dp"
android:layout_height="29dp"
android:text="@string/nike_show"
android:textSize="18sp"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="9dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"/>
<android.support.v4.view.ViewPager
android:id="@+id/item_data_info"
android:layout_width="344dp"
android:layout_height="199dp"
tools:layout_editor_absoluteX="8dp"
tools:ignore="MissingConstraints,NotSibling"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/item_tab"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"/>
<android.support.constraint.ConstraintLayout
android:layout_width="165dp"
android:layout_height="134dp"
android:layout_gravity="center_vertical"
android:scrollbars="none"
tools:ignore="MissingConstraints,RtlHardcoded"
android:id="@+id/constraintLayout2"
tools:layout_editor_absoluteX="98dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/product_name"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/item_tab">
<LinearLayout
android:id="@+id/id_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:layout_editor_absoluteX="50dp"
tools:layout_editor_absoluteY="677dp"
android:layout_below="@id/product_name"
tools:ignore="NotSibling,UnknownId">
</LinearLayout>
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:layout_width="344dp"
android:layout_height="54dp"
android:layout_weight="0.1"
tools:ignore="MissingConstraints,NotSibling"
android:id="@+id/item_tab"
android:layout_below="@+id/id_gallery"
tools:layout_editor_absoluteY="238dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/description"
tools:layout_editor_absoluteX="31dp"
tools:layout_editor_absoluteY="856dp"/>
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/history"
tools:layout_editor_absoluteX="165dp"
tools:layout_editor_absoluteY="856dp"/>
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/alerts"
tools:layout_editor_absoluteX="288dp"
tools:layout_editor_absoluteY="856dp"/>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
ItemPageActivity.java
package com.goldbergtom.cricket;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ItemPageActivity extends AppCompatActivity {
private LinearLayout mGallery;
private int[] mImgIds;
private LayoutInflater mInflater;
private HorizontalScrollView horizontalScrollView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_page);
mInflater = LayoutInflater.from(this);
initData();
initView();
}
private void initData()
{
mImgIds = new int[] { R.drawable.ic_1, R.drawable.ic_2, R.drawable.ic_3,R.drawable.ic_4
};
}
private void initView()
{
mGallery = (LinearLayout) findViewById(R.id.id_gallery);
for (int i = 0; i < mImgIds.length; i++)
{
View view = mInflater.inflate(R.layout.activity_gallery_item,
mGallery, false);
ImageView img = (ImageView) view
.findViewById(R.id.id_index_gallery_item_image);
img.setImageResource(mImgIds[i]);
TextView txt = (TextView) view
.findViewById(R.id.id_index_gallery_item_text);
txt.setText("info "+i);
mGallery.addView(view);
}
}
}
答案 0 :(得分:0)
我需要将对象完全设置为约束规则。