2016-12-22 50 views
0

我有一個關於android佈局的問題。我無法實現我正在尋找的東西,我需要一些方向。 我正在尋找將在滾動視圖佈局內的佈局。它很難解釋,但一張圖片勝過千言萬語。 enter image description hereAndroid的滾動視圖和佈局問題

我想這是一個滾動視圖中,可以在任何幫助嗎?

+0

你想在滾動視圖內的圖像視圖?或者,滾動視圖中的整個佈局? –

回答

0

如果你不想動態添加圖像比這是你想要試試這個佈局。

activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <ScrollView 
     android:id="@+id/sv" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <LinearLayout 
      android:id="@+id/linMain" 
      android:layout_width="match_parent" 
      android:layout_height="fill_parent" 
      android:orientation="horizontal"> 

      <RelativeLayout 
       android:id="@+id/lin1" 
       android:layout_width="100dp" 
       android:layout_height="match_parent" 
       android:layout_margin="10dp"> 

       <ImageView 
        android:id="@+id/imgView1" 
        android:layout_width="100dp" 
        android:layout_height="100dp" 
        android:background="@android:color/holo_purple" /> 

       <ImageView 
        android:id="@+id/imgView2" 
        android:layout_width="100dp" 
        android:layout_height="100dp" 
        android:layout_below="@id/imgView1" 
        android:layout_marginTop="20dp" 
        android:background="@android:color/holo_red_light" /> 

       <ImageView 
        android:id="@+id/imgView3" 
        android:layout_width="100dp" 
        android:layout_height="100dp" 
        android:layout_below="@id/imgView2" 
        android:layout_marginTop="20dp" 
        android:background="@android:color/holo_green_light" /> 

       <ImageView 
        android:id="@+id/imgView4" 
        android:layout_width="100dp" 
        android:layout_height="100dp" 
        android:layout_below="@id/imgView3" 
        android:layout_marginTop="20dp" 
        android:background="@android:color/holo_blue_light" /> 
      </RelativeLayout> 

      <LinearLayout 
       android:id="@+id/lin2" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_margin="10dp" 
       android:orientation="vertical"> 

       <WebView 
        android:id="@+id/webView" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_toRightOf="@id/linImages"></WebView> 

      </LinearLayout> 


     </LinearLayout> 

    </ScrollView> 

</RelativeLayout> 

這裏我附上此佈局UI截圖。

enter image description here

+0

工作!真棒! – sohail

0

試試這個,我已經activity_main包含GridView和網頁流量由兩個佈局..

1)。

2)activity_gridview其中包含imageview將在gridview中膨脹。

我已經使用BaseAdapter將圖像動態添加到Gridview。

在MainActivity中,我已經將Adapter設置爲GridView。試試這個代碼一次。

activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ScrollView 
     android:id="@+id/scrollView" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="false" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:id="@+id/linImages" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal"> 

      <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/grid" 
       android:layout_width="100dp" 
       android:layout_height="match_parent" 
       android:numColumns="1"></GridView> 

      <WebView 
       android:id="@+id/webView" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_toRightOf="@id/linImages"></WebView> 
     </LinearLayout> 


    </ScrollView> 

</RelativeLayout> 

activity_gridview.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <ImageView 
     android:id="@+id/imgView1" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:background="@drawable/star2" /> 

    <ImageView 
     android:id="@+id/imgView2" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:background="@drawable/star1" /> 

    <ImageView 
     android:id="@+id/imgView3" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:background="@drawable/star3" /> 

    <ImageView 
     android:id="@+id/imgView4" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:background="@drawable/star1" /> 

</LinearLayout> 

CustomAdapter.java:

public class CustomAdapter extends BaseAdapter { 
    Context context; 
    int flags[]; 
    LayoutInflater inflter; 

    public CustomAdapter(Context applicationContext, int[] flags) { 
     this.context = applicationContext; 
     this.flags = flags; 
     inflter = (LayoutInflater.from(applicationContext)); 
    } 

    @Override 
    public int getCount() { 
     return flags.length; 
    } 

    @Override 
    public Object getItem(int i) { 
     return null; 
    } 

    @Override 
    public long getItemId(int i) { 
     return 0; 
    } 

    @Override 
    public View getView(int i, View view, ViewGroup viewGroup) { 
     view = inflter.inflate(R.layout.activity_gridview, null); 
     ImageView imgView1 = (ImageView) view.findViewById(R.id.imgView1); 
     ImageView imgView2 = (ImageView) view.findViewById(R.id.imgView2); 
     ImageView imgView3 = (ImageView) view.findViewById(R.id.imgView3); 
     ImageView imgView4 = (ImageView) view.findViewById(R.id.imgView4); 

     imgView1.setImageResource(flags[0]); 
     imgView2.setImageResource(flags[1]); 
     imgView3.setImageResource(flags[2]); 
     imgView4.setImageResource(flags[3]); 
     return view; 
    } 
} 

MainActivity.java:

public class MainActivity extends AppCompatActivity { 

    int images[] = {R.drawable.star2, R.drawable.star1, R.drawable.star3, R.drawable.star1}; 
    GridView grid; 
    CustomAdapter customAdapter; 
    WebView webView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     webView= (WebView) findViewById(R.id.webView); 
     grid = (GridView) findViewById(R.id.grid); 
     customAdapter = new CustomAdapter(getApplicationContext(), images); 
     grid.setAdapter(customAdapter); 
    } 
}