我将为我的管理器应用制作时间表。我的问题是:
- 我已经制作了第一行,然后动态地制作了第二行和其他所有行
- 我有一个layout.xml,但是视图之间的边距不起作用。
我希望将第二行设为蓝色行/第一行。黄色行应该是动态的(在运行时)。
我的源代码:
public class StundenplanActivity extends AppCompatActivity {
TableLayout tl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stundenplan);
//Find the TableLayout defined in activity_stundenplan
tl = (TableLayout)findViewById(R.id.myTableLayout);
createRow();
}
public void createRow(){
/* Create a new row to be added. */
LayoutInflater inflater = getLayoutInflater();
TableRow tr = (TableRow) inflater.inflate(R.layout.tablerow_tabelle,null);
String[] day = {"Zeit", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"};
TextView[] spalte = new TextView[8];
for(int i=0; i<=7; i++){
TextView text = (TextView) inflater.inflate(R.layout.text_view_tabelle, null);
text.setText(day[i]);
spalte[i] = text;
}
for(int i=0; i<=7; i++) {
tr.addView(spalte[i]);
}
tl.addView(tr);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_stundenplan, menu);
return true;
}
public boolean onOptionItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.item_einstellungen_stundenplan) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
动态文字视图的布局xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:background="#f0d000"
android:padding="@dimen/stundenplanactivity_padding"
android:layout_marginRight="@dimen/stundenplanactivity_marginRight"
android:layout_marginTop="@dimen/stundenplanactivity_marginTop">
</TextView>
编辑:用于tablerow_tabelle的xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1cf000">
</TableRow>
第一行的xml(不是动态的):
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myTableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/mainactivity_horizontal_margin"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text0"
android:text="@string/Zeit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="@dimen/stundenplanactivity_padding"
android:layout_marginRight="@dimen/stundenplanactivity_marginRight"
android:layout_marginTop="@dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="@+id/text1"
android:text="@string/Wochentag0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00f0c0"
android:padding="@dimen/stundenplanactivity_padding"
android:layout_marginRight="@dimen/stundenplanactivity_marginRight"
android:layout_marginTop="@dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="@+id/text2"
android:text="@string/Wochentag1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="@dimen/stundenplanactivity_padding"
android:layout_marginRight="@dimen/stundenplanactivity_marginRight"
android:layout_marginTop="@dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="@+id/text3"
android:text="@string/Wochentag2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="@dimen/stundenplanactivity_padding"
android:layout_marginRight="@dimen/stundenplanactivity_marginRight"
android:layout_marginTop="@dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="@+id/text4"
android:text="@string/Wochentag3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="@dimen/stundenplanactivity_padding"
android:layout_marginRight="@dimen/stundenplanactivity_marginRight"
android:layout_marginTop="@dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="@+id/text5"
android:text="@string/Wochentag4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="@dimen/stundenplanactivity_padding"
android:layout_marginRight="@dimen/stundenplanactivity_marginRight"
android:layout_marginTop="@dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="@+id/text6"
android:text="@string/Wochentag5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="@dimen/stundenplanactivity_padding"
android:layout_marginRight="@dimen/stundenplanactivity_marginRight"
android:layout_marginTop="@dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="@+id/text7"
android:text="@string/Wochentag6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="@dimen/stundenplanactivity_padding"
android:layout_marginRight="@dimen/stundenplanactivity_marginRight"
android:layout_marginTop="@dimen/stundenplanactivity_marginTop"
/>
</TableRow>
</TableLayout>
答案 0 :(得分:1)
此处此代码可以很好地为不同的行进行不同的着色。好吧,它可能对你有所帮助。
在我的代码中,我使用了两行。第一个表格布局是标题行,第二个表格布局是值行。我设置标题的背景作为背景图像和具有背景颜色的值布局。
在XML布局中:
<div ng-repeat='a in array'>
<div ng-if="a.type =='event'">
// EVENT.html TEMPLATE
</div>
<div ng-if="a.type == 'alert'">
// ALERT.html TEMPLATE
</div>
</div>
在Java代码中:
<LinearLayout
android:id="@+id/gridValue1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TableLayout
android:id="@+id/GridFrozenTable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="1dip"
android:layout_marginTop="2dip"
android:stretchColumns="*"
/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="1dip"
android:layout_marginRight="1dip"
android:layout_marginTop="2dip"
android:layout_toRightOf="@id/GridFrozenTable">
<TableLayout
android:id="@+id/GridContentTable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:stretchColumns="*" />
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
当然,我可以帮助你!! 快乐编码!!
答案 1 :(得分:0)
使用布局XML文件设置边距。
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"/>
要更改某些特定的边距,请使用此代码。
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"
答案 2 :(得分:0)
这可以帮助
TextView txtvew= (TextView) findViewById(R.id.ForgotPasswordText);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(50, 0, 0, 0)
txtvew.setLayoutParams(llp);