android.widget.LinearLayout 不能转换为 android.widget.TableRow



我正在尝试将行添加到表格布局中。但是 android.widget.LinearLayout 不能转换为 android.widget.TableRow 错误。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:layout_height="wrap_content"
tools:context="com.example.khash.santotootsoolol.MainActivity"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="horizontal"
android:fillViewport="true">
<TableLayout
android:id="@+id/table_result"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView
android:text="Date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:background="@drawable/table_header"
android:textColor="@android:color/white"/>
<TextView
android:text="Payment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:background="@drawable/table_header"
android:textColor="@android:color/white"/>

<TextView
android:text="Remainder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:background="@drawable/table_header"
android:textColor="@android:color/white"/>

<TextView
android:padding="10dp"
android:text="Interest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/table_header"
android:textColor="@android:color/white" />
</TableRow>

</TableLayout>
</HorizontalScrollView>
</LinearLayout>

和我的活动

public class MainActivity extends AppCompatActivity {
private TableLayout tableLayout = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tableLayout = (TableLayout)findViewById(R.id.table_result);
TableRow tr = (TableRow)LayoutInflater.from(this).inflate(R.layout.table_row, null);
tableLayout.addView(tr);
setContentView(R.layout.activity_main);}
}

我的自定义表行是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4"
android:layout_marginBottom="3dp">
<TextView
android:id="@+id/tv_pay_date"
android:text="7/31/2017"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:background="@drawable/table_row_item"/>
<TextView
android:id="@+id/tv_monthly_payment"
android:text="13,933,700"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:background="@drawable/table_row_item"/>

<TextView
android:id="@+id/tv_remainder_payment"
android:text="125,403,300"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:background="@drawable/table_row_item"/>

<TextView
android:id="@+id/tv_interest"
android:text=""
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:background="@drawable/table_row_item"/>
</LinearLayout>

我改变了线性布局并膨胀,但仍然发生错误。或者我不能在表格布局中膨胀自定义布局?

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4"
android:layout_marginBottom="3dp">
<TextView
android:id="@+id/tv_pay_date"
android:text="7/31/2017"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:background="@drawable/table_row_item"/>
<TextView
android:id="@+id/tv_monthly_payment"
android:text="13,933,700"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:background="@drawable/table_row_item"/>

<TextView
android:id="@+id/tv_remainder_payment"
android:text="125,403,300"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:background="@drawable/table_row_item"/>

<TextView
android:id="@+id/tv_interest"
android:text=""
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:background="@drawable/table_row_item"/>
</TableRow>

希望这有帮助。

相关内容

最新更新