GridLayout将customView添加为子问题Android

时间:2017-07-03 18:33:38

标签: java android android-custom-view layoutparams

我使用GridLayout制作了一个简单的时间表,它看起来像这样 enter image description here

现在的想法是将所需主题插入特定的行和列。为了实现这一点,我创建了一个扩展CardView的类,我需要在其中插入TextView。 码: TimeTable.xml

<GridLayout 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:columnCount="8"
    android:columnOrderPreserved="true"
    android:rowCount="6"
    tools:context="com.digiart.schoolapp.fragments.TimetableFragment">


    <android.support.v7.widget.CardView
        android:id="@+id/timetable_card_space"

        android:layout_column="0"
        android:layout_columnSpan="1"
        android:layout_columnWeight="1"
        android:layout_row="0"
        app:cardElevation="2dp"
        app:contentPadding="5dp">

        <TextView
            android:id="@+id/timetable_dummy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="time"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:visibility="invisible" />
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:id="@+id/timetable_day1_card"
        android:layout_column="1"
        android:layout_columnWeight="1"
        android:layout_row="0"
        app:cardElevation="2dp"
        app:contentPadding="5dp">

        <TextView
            android:id="@+id/timetable_day1_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Monday"
            android:textAppearance="@style/TextAppearance.AppCompat.Body2" />
    </android.support.v7.widget.CardView>

    .....
    ........
    <android.support.v7.widget.CardView
        android:id="@+id/timetable_time1_card"
        android:layout_column="0"
        android:layout_columnWeight="1"
        android:layout_row="1"
        app:cardElevation="2dp"
        app:contentPadding="5dp">

        <TextView
            android:id="@+id/timetable_time1_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="09.00"
            android:textAppearance="@style/TextAppearance.AppCompat.Body2" />
    </android.support.v7.widget.CardView>
    ..... 
    ......
   </GridLayout>

TimetableSubject:

public class TimetableSubject extends CardView {

    TextView subjectText;

    public TimetableSubject(Context context,int column,int row,int columnSpan,String subjectName) {
        super(context);

        GridLayout.LayoutParams subjectParam =new GridLayout.LayoutParams();
        subjectParam.columnSpec = GridLayout.spec(column,columnSpan);
        subjectParam.rowSpec = GridLayout.spec(row);

        subjectText = new TextView(context);
        CardView.LayoutParams textParams = new CardView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        subjectText.setText(subjectName);
        subjectText.setLayoutParams(textParams);

        setLayoutParams(subjectParam);
    }


}

现在我得到了我需要做的事情,我需要将行和列传递给自定义视图,并将它们设置为布局参数。我认为问题在于布局参数代码,我必须在那里弄乱一些东西。任何人都可以解释如何正确设置这种情况下的布局参数吗?感谢。

2 个答案:

答案 0 :(得分:0)

你应该使用TableLayout。在TableLayout中,您可以创建任意数量的行和列。浏览TableLayout文档并阅读行和列的概念。这是一个非常好的桌面布局教程。

tutorial对您也很有帮助。

答案 1 :(得分:0)

Spec中的GridLayout权重参数存在问题。通过在Spec中将weight作为float参数插入来解决问题 代码:

int Socket=socket(AF_INET, SOCK_DGRAM,0);
/*FD_ZERO(&SockSet);
FD_SET(Socket,&SockSet);
sTimeval.tv_sec=0.1;
sTimeval.tv_usec=0;
int status=select(Socket+1,&SockSet,(fd_set*)NULL,(fd_set *)NULL,&sTimeval);
cout<<status<<endl;*/

if(Socket!=-1)
cout<<"Socket created"<<endl;
else
cout<<"Socket not created"<<endl;

unsigned short port=4012;

struct in_addr serverIP;
//(void)inet_pton(AF_INET,"192.168.56.100", &clientIP);
(void)inet_pton(AF_INET,"192.168.56.101", &serverIP);

struct sockaddr_in server;
memset(&server,0, sizeof(server));
server.sin_family=AF_INET;
server.sin_addr=serverIP;
server.sin_port=htons(port);


if(bind(Socket,(struct sockaddr*)&server, sizeof(&server))!=1)
cout<<"Binding successful"<<endl;
else
cout<<"Binding failed";


struct sockaddr client;
memset(&client, 0, sizeof(client));
socklen_t clientlen=0;
struct sockaddr_in* client_in;
memset(&client_in,0,sizeof(client_in));


cout<<"warten vor recv"<<endl;
ssize_t bytesread=recvfrom(Socket, &msg, sizeof(msg), 0, &client,&clientlen);
cout<<(int)bytesread<<endl;
cout<<msg<<endl;


client_in=(struct sockaddr_in*)&client;
char* client_adr=inet_ntoa(client_in->sin_addr);
printf("%s Port%d\n", client_adr, ntohs(client_in->sin_port));


if (bytesread == -1) {
cerr << "Fehler beim empfangen" << endl;
int status = close(Socket);
if (status == 0)
cout << "Socket closed" << endl;
else if (status == -1)
cout << "Socket not closed" << endl;
return (1);

}


//Wenn Infos ueber Client nicht schon vorher aus recvfrom()
/*struct in_addr clientIP;
(void)inet_pton(AF_INET,"192.168.2.102", &clientIP);
struct sockaddr_in client;
memset(&client,0, sizeof(client));
client.sin_family=AF_INET;
client.sin_addr=clientIP;
client.sin_port=htons(portwindows);*/


char msg2[]="12345";
ssize_t bytessent=sendto(Socket,msg2,strlen(msg2),0,(struct sockaddr*)&client,sizeof(client));
cout<<(int)bytessent<< " bytes were sent"<<endl;



int status=close(Socket);
if(status==0)
cout<<"Socket closed"<<endl;
else if(status==-1)
cout<<"Socket not closed"<<endl;