android.support.constraint.ConstraintLayout error



错误说我需要在我的代码中验证android:layout_height/width,并且它没有android:layout_height/width的必需元素。Java新手,因此任何帮助将不胜感激。

我的主要活动代码是这样的:

package com.example.name.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

我的activity_main.xml代码在这里:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.name.myapplication.MainActivity">
<TableLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/table_main"
android:layout_marginTop="48dp"
android:layout_width="match_parent"
android:layout_height="48dp"
android:stretchColumns="1">
<TableRow>
<TextView
android:layout_column="1"
android:text="@string/name"
android:padding="3dip" />
<TextView
android:text="@string/address"
android:padding="3dip" />
<TextView
android:text="@string/phone"
android:gravity="right"
android:padding="3dip" />
<TextView
android:text="@string/fax"
android:gravity="right"
android:padding="3dip"
android:layout_height="45dp"/>
</TableRow>
</TableLayout>
</android.support.constraint.ConstraintLayout>

您似乎尚未向约束布局添加任何高度或宽度约束。因此,您的应用程序不知道要进行此布局的高度或宽度。

尝试在约束布局下添加以下内容:

android:layout_width="match_parent"
android:layout_height="match_parent"

所以你的代码将如下所示:

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.name.myapplication.MainActivity">

最新更新