【问题标题】:XML organization problemsXML 组织问题
【发布时间】:2016-07-13 16:52:28
【问题描述】:

我遇到了如何组织 XML 文件如下图所示的问题。

我已经花了足够的时间,我做不到。如果你能帮助我我应该做什么配置。如何组织没有限制。但是如果可以教我新方法而不是亲子。

我想变成这样的画面:

我的 XML 是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="horizontal"
    tools:context="com.ahmed.bluetooth.DataAnalysis">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Peak Pressures"
        android:layout_weight="1"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Time in Sec."
        android:layout_weight="1"/>




<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_weight="1">
    <TextView
        android:id="@+id/text_Index"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/Peaks_index"
        android:textColor="#040307"
        android:scrollbars="vertical"/>
    <TextView
        android:id="@+id/Peak_Num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Peaks"
        android:textColor="#040307"
        android:scrollbars="vertical"/>
    <Button
        android:id="@+id/button_Del"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Delete File"
        android:background="@drawable/button"
        android:layout_margin="1dp" />
</LinearLayout>

</LinearLayout>

xml现在看起来像这样,但我想要它作为图片:

【问题讨论】:

  • 你需要把你的组件排列成第一张图吗??

标签: android xml android-layout android-studio textview


【解决方案1】:

这是我的建议(TableLayout):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ahmed.bluetooth.DataAnalysis">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.6"
        android:stretchColumns="*" >

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Peak Pressures"
                android:layout_column="0" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Time in Sec."
                android:layout_column="1" />
            <Button
                android:id="@+id/button_Del"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Delete File"
                android:background="@drawable/button"
                android:layout_margin="1dp"
                android:layout_column="3" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/text_Index"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/Peaks_index"
                android:textColor="#040307"
                android:scrollbars="vertical"
                android:layout_column="0" />
            <TextView
                android:id="@+id/Peak_Num"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Peaks"
                android:textColor="#040307"
                android:scrollbars="vertical"
                android:layout_column="1" />

        </TableRow>
    </TableLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.4">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Empty area"
            android:id="@+id/textView2"
            android:layout_gravity="center" />
    </LinearLayout>
</LinearLayout>

【讨论】:

    【解决方案2】:

    这里是您答案的完整解决方案

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:weightSum="5"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="2"
        android:background="#efefef">
    
        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:padding="20dp">
    
            <TextView
                android:layout_width="112dp"
                android:layout_height="32dp"
                android:text="Peak Preassure"
                android:id="@+id/textView"
                android:layout_row="1"
                android:layout_column="0"
                android:textColor="#000000" />
    
            <TextView
                android:layout_width="112dp"
                android:layout_height="32dp"
                android:text="Time in sec"
                android:id="@+id/textView2"
                android:layout_row="1"
                android:layout_column="1"
                android:textColor="#000000" />
    
            <Button
                android:layout_width="112dp"
                android:layout_height="32dp"
                android:text="Delete"
                android:id="@+id/btn"
                android:layout_row="1"
                android:layout_column="2"
                android:textColor="#000000" />
    
            <TextView
                android:layout_width="112dp"
                android:layout_height="32dp"
                android:text="Peak num"
                android:id="@+id/textView3"
                android:layout_row="2"
                android:layout_column="0"
                android:textColor="#000000" />
    
            <TextView
                android:layout_width="112dp"
                android:layout_height="32dp"
                android:text="Peak index"
                android:id="@+id/textView4"
                android:layout_row="2"
                android:layout_column="1"
                android:textColor="#000000" />
    
        </GridLayout>
    </RelativeLayout>
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="3"
        android:background="#dddddd">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Empty area for future"
            android:id="@+id/textView5"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:textColor="#000000" />
    </RelativeLayout>
    


    说明

    1. 首先使用orientation = vertical中的LinearLayout,这样你就可以通过垂直分割屏幕来放置另外2个(或更多)布局(占据你的textViews和你未来的区域)
    2. 然后在父布局中使用weightSum 属性和在智利布局中使用layoutweight 属性来定义比率(这里我给了上部2/5 下部3/5)
    3. 使用GridLayoutTablelayout 占据您的元素
    4. 进一步使用paddinggravity 等属性来改进您的用户界面

    【讨论】:

    • 非常感谢。设计不错。
    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多