android.widget.LinearLayout无法强制转换为android.widget.TableRow

时间:2017-07-04 03:49:55

标签: android android-tablelayout

我正在尝试向tablelayout添加行。但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>

我改变了线性布局和inflater,但仍然出现错误。或者我不能在表格布局中夸大自定义布局?

1 个答案:

答案 0 :(得分:1)

试试这个:

<?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>

希望这有帮助。