![](/img/trans.png)
[英]how to set different actions to dynamiclly created button on android (specifically - auto scroll to a textview using a dynamic button)
[英]How to set the margin of a textview dynamiclly?
我將為我的管理器應用程序制定時間表。 我的問題是:
-我已經制作了第一行,並像這樣但又動態地制作了第二行和每隔一行。
-為此,我有一個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:
<?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>
在這里,此代碼可以很好地為不同的行進行不同的着色。 好吧,可能會對您有所幫助。
在我的代碼中,我使用了兩行。 第一個表格布局是標題行,第二個表格布局是值行。 我將標題的背景設置為背景圖像和具有背景顏色的值布局。
在XML布局中:
<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>
在Java代碼中:
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void PopulateMainTable(ActionCodeReport[] content) {
TableRow HeaderRow = new TableRow(this);
HeaderRow.setPadding(0, 0, 0, 2);
TextView HeaderCell1 = new TextView(this);
HeaderCell1.setText("Action code ");
HeaderCell1.setTextColor(Color.BLACK);
HeaderCell1.setHeight(65);
HeaderCell1.setGravity(Gravity.CENTER_HORIZONTAL);
HeaderCell1.setBackgroundResource(R.drawable.lt); //Here i set background image for header
HeaderRow.addView(HeaderCell1);
TextView HeaderCell2 = new TextView(this);
HeaderCell2.setText("NoOfAccnt");
HeaderCell2.setHeight(65);
HeaderCell2.setBackgroundResource(R.drawable.lt); //Same image
HeaderCell2.setGravity(Gravity.CENTER_HORIZONTAL);
HeaderRow.addView(HeaderCell2);
HeaderCell2.setTextColor(Color.BLACK);
TableRow contentRow = new TableRow(this);
contentRow.setPadding(0, 0, 0, 2);
int orientation = this.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
TextView Cell1 = new TextView(this);
Cell1.setText(r.getAction_Code());
Cell1.setTextColor(Color.BLACK);
Cell1.setGravity(Gravity.LEFT);
Cell1.setBackgroundColor(Color.LTGRAY); //**Here change your background color as you need**
contentRow.addView(Cell1);
TextView Cell2 = new TextView(this);
Cell2.setText(String.valueOf(r.getNoOfAccount()));
Cell2.setTextColor(Color.BLACK);
Cell2.setGravity(Gravity.CENTER_HORIZONTAL);
contentRow.addView(Cell2);
contentTable.addView(contentRow);
}
當然,我可以幫助您! 快樂編碼!
使用布局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"
這可以幫助
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.