![](/img/trans.png)
[英]Java Android: the button does not work after returning to the first layout
[英]Button layout_alignParentRight does not work on the first loop?
我動態添加了viewflipper,並在每個視圖上添加了多個視圖,其中一個是包含兩個按鈕的RelativeLayer-一個位於圖層右側,位於左側。
它適用於所有循環,但不適用於第一個循環,在這兩個按鈕都位於左側的另一個按鈕上。 任何有幫助的建議的人都表示贊賞。
LayoutInflater inflater = LayoutInflater.from(this);
ViewGroup parent = (ViewGroup) findViewById(R.id.slider_holder);
for (int i = 0; i < temp.length; i++) {
ViewGroup np = (ViewGroup) findViewById(R.id.nexpre);
LinearLayout l=(LinearLayout) inflater.inflate(R.layout.flipper_slides, parent, false);
RelativeLayout next_prev =(RelativeLayout) inflater.inflate(R.layout.next_prev, l, false);
for (int k = 0; k< temp1.length; k++) {
ViewGroup nested_parent = (ViewGroup) findViewById(R.id.nested_slider_holder);
LinearLayout nl=(LinearLayout) inflater.inflate(R.layout.nested_flipper_slides, nested_parent, false);
TextView temp_tv1 =(TextView) inflater.inflate(R.layout.nested_flipper_tv, nl, false);
temp_tv1.setText(temp[i]+" "+temp1[k]);
nl.addView(temp_tv1);
ff.addView(nl);
}
next_prev.addView(nested_prev);
next_prev.addView(nested_next);
l.addView(ff);
l.addView(next_prev);
f.addView(l);
}
這是next_prev.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/nexpre"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_marginTop="-50dip" >
</RelativeLayout>
這是nested_prev.xml
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Prev" >
</Button>
這是nested_next.xml
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Next" >
</Button>
問題是,在第一個循環上,應該一直向右移動的按鈕沒有。 對於第一個循環之后的所有其他循環,它起作用,但對第一個循環無效。
我將此行添加到代碼中:
if(i==0){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
next_prev.getChildAt(1).setLayoutParams(params);
}
對於第一個循環,我使用布局參數以編程方式執行“ ALIGN PARENT RIGHT”。
無論如何,如果有人能解釋第一個循環與其他循環表現不同的原因,我將不勝感激。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.